Matching Date formatted: "January 17, 2017 10:30 AM" in Ruby - ruby

I have been trying to use Date/DateTime to validate that a given date is in the correct format.
str = "January 17, 2017 10:30 AM"
temp = DateTime.strptime(str, '%B %-d, %y %l:%M %p')
but am getting the error
`strptime': invalid date (ArgumentError)
I have been able to split the string into ""January 17," "2017 10:30 AM" and validate it without issue, but I would really like to know why I can't just use strptime on the whole string, or what I am doing wrong if it can be done.

This error is happening because according to the docs of DateTime#strptime:
Parses the given representation of date and time with the given template, and creates a date object. strptime does not support specification of flags and width unlike strftime.
And your format includes a value of %-d which is a width parameter, hence the exception. If you try a basic invocation like:
DateTime.strptime(str, '%B %d, %Y')
you'll see it works. Also, you'll want uppercase-Y for the full 4-digit year.
In a nutshell: you'll need to adjust your format string

This format works fine :
temp = DateTime.strptime(str, '%B %d, %Y %l:%M %p')
#<DateTime: 2017-01-17T10:30:00+00:00 ((2457771j,37800s,0n),+0s,2299161j)>

Related

Error using strptime, strftime while converting date to epoch

I am trying to extract dates from csv file and convert them to epoch time
CSV.foreach(File.path("month.csv")) do |row|
dateper=row[0].split(',')[0]
p DateTime.strptime(dateper,"%m/%d/%y %I:%M:%S %p %z").strftime("%s")
end
I am getting invalid date (ArgumentError) as the result. However, if I print out dateper, choose one of the outputs randomly and cop-paste it in place of dateper in line 3, I get an epoch value. What am I doing wrong? dateper is of string value, and strptime minus strftime seems to give an output.
month.csv sample:
7/26/13 12:00:00 AM -05:00,62.2,63.02,62.07,63.02,5.00168E+07
8/23/13 12:00:00 AM -05:00,71.84,71.93,71.36,71.6,5.55304E+07
8/26/13 12:00:00 AM -05:00,71.56,72.91,71.52,71.87,8.26536E+07
8/27/13 12:00:00 AM -05:00,71.16,71.81,69.49,69.82,1.058488E+08
Could someone help me with this?
You have some malformed dates somewhere. Use rescue to avoid the script choking on them.
CSV.foreach(File.path("month.csv")) do |row|
dateper=row[0].split(',')[0]
p DateTime.strptime(dateper,"%m/%d/%y %I:%M:%S %p %z").strftime("%s") rescue p "malformed date:#{dateper} on line #{$.}"
end
This will allow the script to continue and show you the bad dates.

Rails DateTime gives invalid date sometimes and not others

I've got a bunch of user-inputted dates and times like so:
date = "01:00pm 06/03/2015"
I'm trying to submit them to a datetime column in a database, and I'm trying to systemize them like this:
DateTime.strptime(date, '%m/%d/%Y %H:%M')
But I consistently get an invalid date error. What am I doing wrong? If I submit the string without strptime the record will save but it sometimes gets the date wrong.
Also, how can I append a timezone to a DateTime object?
Edit:
So .to_datetime and DateTime.parse(date) work for the date string and fail for date2. What's going on?
date2 = "03:30pm 05/28/2015"
Try using to_datetime:
date.to_datetime
# => Fri, 06 Mar 2015 13:00:00 +0000
Also if you read the documentation for DateTime#strptime, here. It states:
Parses the given representation of date and time with the given
template, and creates a date object.
Its important to note that the template sequence must match to that of input string sequence, which don't in your case - leading to error.
Update
Using to_datetime over second example will generate
ArgumentError: invalid date
This is because it expects the date to be in dd-mm-yy format. Same error will be raised for DateTime.parse as to_datetime is nothing but an api for the later. You should use strptime in-case of non-standard custom date formats. Here:
date2 = "03:30pm 05/28/2015"
DateTime.strptime(date2, "%I:%M%p %m/%d/%Y")
# => Thu, 28 May 2015 15:30:00 +0000
date = "01:00pm 06/03/2015"
DateTime.parse(date)
=> Fri, 06 Mar 2015 13:00:00 +0000
You haven't got your parameters in the correct order.
DateTime.strptime(date, '%H:%M%p %m/%d/%Y')
You'll also need to add %p for the am/pm suffix

convert ruby date to string without losing format

I'm trying to convert a Ruby Date object to a string. The format of the date is: Sun, 15 Sep 2013
However, when I convert it to a string using #to_s it gives me the following: "2013-09-15"
Instead, I want it to become: "Sun, 15 Sep 2013"
Use Date#strftime there are so many options
require 'date'
date = Date.parse("Sun, 15 Sep 2013") # => #<Date: 2013-09-15 ((2456551j,0s,0n),+0s,2299161j)>
date.strftime("%a, %d %b %Y") # => "Sun, 15 Sep 2013"
strftime works well, however, if you find that you're using the same format in multiple places, you will find using the Rails Date#to_formatted_s method a more appropriate option. You can use the built-in formats:
date.to_formatted_s(:short)
date.to_formatted_s(:long)
or, you can create your own formats, adding them to Date::DATE_FORMATS:
Date::DATE_FORMATS[:my_format] = '%a, %d %b %Y'
date.to_formatted_s(:my_format)
This will keep you from spreading formatting strings throughout your app.

Rails 4 parse a date in a different language

I have a text_field :birthday_line in my user form, that I need to parse into the user's birthday attribute.
So I'm doing something like this in my User class.
attr_accessor :birthday_line
before_save :set_birthday
def set_birthday
self.birthday = Date.strptime(birthday_line, I18n.translate("date.formats.default")
end
But the problem is that for some reason it gives me an error saying Invalid date when I try to pass in a string 27 января 1987 г. wich should be parsed to 1987-01-27.
The format and month names in my config/locales/ru.yml
ru:
date:
formats:
default: "%d %B %Y г."
month_names: [~, января, февраля, марта, апреля, мая, июня, июля, августа, сентября, октября, ноября, декабря]
seem to be correct.
Date.parse also doesn't help, it just parses the day number (27) and puts the month and year to todays date (so it'll be September 27 2013 instead of January 27 1987).
I had the same problem and what I can suggest:
string_with_cyrillic_date = '27 Января 1987'
1)create array of arrays like this
months = [["января", "Jan"], ["февраля", "Feb"], ["марта", "Mar"], ["апреля", "Apr"], ["мая", "May"], ["июня", "Jun"], ["июля", "Jul"], ["августа", "Aug"], ["сентября", "Sep"], ["октября", "Oct"], ["ноября", "Nov"], ["декабря", "Dec"]]
2) Now you can iterate this and find your cyrillic month:
months.each do |cyrillic_month, latin_month|
if string_with_cyrillic_date.match cyrillic_month
DateTime.parse string_with_cyrillic_date.gsub!(/#{cyrillic_month}/, latin_month)
end
end
And now you will receive the date that you expect
27 Jan 1987

What am I doing wrong with DateTime.strptime?

My ruby program says that my date is invalid when I do that:
format = "%D/%M/%Y %H:%M:%S:3N"
date = "21/03/2011 16:39:11.642"
DateTime.strptime(time, format)
I have also tried this one:
format = "%D/%M/%Y %H:%M:%S:3"
All I get is this:
ArgumentError: invalid date
from /usr/local/lib/ruby/1.9.1/date.rb:1688:in `new_by_frags'
from /usr/local/lib/ruby/1.9.1/date.rb:1713:in `strptime'
from (irb):12
from /usr/local/bin/irb:12:in `<main>'
It looks like you were getting strptime's format directives confused. Notice how %M is in format twice, once representing the month and the next time representing the minute?
%D means the date as %m / %d / %y.
%d means the day of the month [01,31]
%M means the minute [00,59]
%m means the month number [01,12]
This should work:
format = "%d/%m/%Y %H:%M:%S"
date_time = "21/03/2011 16:39:11.642"
puts DateTime.strptime(date_time, format) #=> 2011-03-21T16:39:11+00:00
Here's a strptime reference
Try to use
datetime.to_date.strftime(format)
or
datetime.to_time

Resources