Date.strptime throwing invalid date for "%Y%W" format - ruby-on-rails

My date = 1st January, 2015.
I did
date_formatted = date.strftime('%Y%W') which returned "201500"
Then when am trying to do Date.strptime(date_formatted, '%Y%W') it's throwing an invalid date error even though the original string was returned from strptime's counterpart srftime.

Found the solution. Time.strptime(date_formatted, '%Y%W') is what will work here.

Related

How to modify a date to meet correct groovy standard?

I am trying to parse the following date in Jenkins 2021-10-14T18:12:20.578+00:00 however, I am getting the error Unparseable date: "2020-01-01T10:10:20.578+00:00"
This is my code, not sure what I am doing wrong:
Date myDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2020-01-01T10:10:20.578+00:00");
EDIT:
Thanks to Kaus, I found that my date is not formatted properly and should be 2020-01-01T10:10:20.578GMT+00:00
I'm getting this date from some other files. I can replace + with GMT+ as follow:
def myDate = "2020-01-01T10:10:20.578+00:00"
myDate = myDate.replaceAll("\\+", "GMT\\+")
How can I do the same thing if my date is "2020-01-01T10:10:20.578-06:00"
The following is replacing every "-"
def myDate = "2020-01-01T10:10:20.578-06:00"
myDate = myDate.replaceAll("\\+", "GMT\\+").replaceAll("\\-", "GMT\\-")
Output: "2020GMT-01GMT-01T10:10:20.578GMT-06:00"
Use X for ISO8601 time zone, instead of Z for RFC 822 time zone.
(from https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
import java.text.SimpleDateFormat
Date myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.parse("2020-01-01T10:10:20.578+00:00")
Missing GMT there
Date myDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2020-01-01T10:10:20.578GMT+00:00");

Converting a string into a date Ruby on Rails

I'm attempting to convert a string date into a date that can be stored in the database. These are my attempts:
params[:event][:start_date] = '03-21-2016'
DateTime.strptime(params[:event][:start_date], '%Y-%m-%dT%H:%M:%S%z')
or
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y')
However I keep getting an invalid date error. I'm not sure what I'm doing wrong.
One way to do it would be this:
date_string = '03-21-2016'
month, day, year = date_string.split('-').map(&:to_i)
DateTime.new(year, month, day)
You need to understand what each fragment (eg %Y) in the format string ('%Y-%m-%dT%H:%M:%S%z') means: read this.
http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime
Once you know this, you can tailor a format string to the date string you have, or expect to get: in this case, "%m-%d-%Y".
When debugging create a new, basic and simple, version of the code and test that.
require 'date'
params = '03-21-2016'
DateTime.strptime(params, '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>
Note the order for the format: '%m-%d-%Y', which works. That's the problem with your first attempt, where you tried to use%Y-%m-%d. There is NO month21or day2016`.
Your second attempt is valid but your question makes it appear it doesn't work. You need to be more careful with your testing:
params = {event:{start_date:'03-21-2016'}}
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>

Parsing date time in D3.js

I'm trying to parse the date time value from .csv file in D3.js, but for some reason I keep getting null. Here's what I have,
.csv data:
"","date","count"
"1","2011-11-15 02:00:01",1
"2","2011-11-15 02:00:02",2
"3","2011-11-15 02:00:03",1
"4","2011-11-15 02:00:04",1
"5","2011-11-15 02:00:05",1
"6","2011-11-15 02:00:07",1
var parseDate = d3.time.format("%I:%M %p").parse;
When I do console.log(parseDate(d.date)), I get null. I also tried console.log(parseDate(d.date.toString())), but I still get null. Why is it not parsing?

Calabash iOS Date picker issue

I am using a simple code for date picker for my Calabash-iOS automation. Whenever I set the today's or some past date to select, it is failed. I hope someone would have the idea about it. Here is my code and error messages:
Then(/^I set Reminder Date "(.?)" Time "(.?)"$/) do |date10, time10|
target_time = Time.parse(time10)
target_date = Date.parse(date10)
current_time = date_time_from_picker()
current_date = date_time_from_picker()
date_time = DateTime.new(target_date.year,
target_date.mon,
target_date.day,
target_time.hour,
target_time.min,
0,
target_time.gmt_offset)
picker_set_date_time date_time
sleep(3)
end
Then I set Reminder Date "05/08/2014" Time "5:24"
If the set date is todays date or a past date, this fails and if it is some future date, it passes. If some friends knows its solution, please suggest.
Check the minimum date of your UIDatePicker.
http://calabashapi.xamarin.com/ios/Calabash/Cucumber/DatePicker.html#picker_set_date_time-instance_method
You mention that 'it fails'. Can you describe how it fails?
If the minimum date turns out to be the problem and a Runtime error is not raised, please file a bug.
(RuntimeError) — if the target date is less than the picker's minimum date

DateTime from WebApi to Breeze is transformed with the Time Localization

I have a form on which I set a start Date and a finish Date for a entity.
On the Web Api side, before saving the date to the database,I set the start date: 2013-09-25 00:00:00.000 and the the end date as 2013-09-26 23:59:59.000.
var vote = (VotingSet)Entity;
vote.Start = new DateTime(vote.Start.Year, vote.Start.Month, vote.Start.Day, 0, 0, 0, 0);
vote.End = new DateTime(vote.End.Year, vote.End.Month, vote.End.Day, 23, 59, 58);
This is from the JSON that is send to the rest service looks like this:
Start: "2013-09-25T00:00:00.000Z"
End: "2013-09-26T00:00:00.000Z"
After the save, in the javascript client, the entity is updated with the new key and with the properties that come from the server.
The observable date objects will have the following value
Start: Wed Sep 25 2013 03:00:00 GMT+0300 (GTB Daylight Time)
End: Fri Sep 27 2013 02:59:58 GMT+0300 (GTB Daylight Time)
This is what i am getting back from the server
Start: "2013-09-25T00:00:00.000"
End: "2013-09-26T23:59:58.000"
How can i make sure that the hours in my object are not modified?
EDIT:
There is a a good explaniation here on what's happening with the datetime in javascript.
In the end i used this snipped to solve my problem:
breeze.DataType.parseDateFromServer = function (source) {
var date = moment(source);
return date.toDate();
};
It override's breeze own function with adds a time offset to the datetime.
Breeze does not manipulate the datetimes going to and from the server in any way EXCEPT to add a UTZ timezone specifier to any dates returned from the server that do not already have one. This is only done because different browsers interpret dates without a timezone specifier differently and we want consistency between browsers.
This is discussed in more detail in the answer posted here.
You are passing ISO8601 formatted timestamps, which is good. When you pass the Z at the end, you are indicating that the timestamp represents UTC. When you load those into JavaScript, it's going to take that into account.
You still need to show more code if you are looking for a useful response. What you've currently described from .NET doesn't quite line up with the timestamps you've provided. And it seems like most of this problem has to do with JavaScript and you haven't yet shown any of that code, so I can only guess what you might be doing. Please update your question, and understand that we have no knowledge of your system other than what you show us.
It's possible you may find moment.js to be useful in this scenario, but I can't elaborate further without seeing the relevent JavaScript code.

Resources