Parse a date in rails Parse a date in rails ruby-on-rails ruby-on-rails

Parse a date in rails


You can try Date.parse(date_string).

You might also use Date#strptime if you need a specific format:

> Date.strptime("10/15/2013", "%m/%d/%Y")=> Tue, 15 Oct 2013

For a general solution:

format_str = "%m/%d/" + (date_str =~ /\d{4}/ ? "%Y" : "%y")date = Date.parse(date_str) rescue Date.strptime(date_str, format_str)


I find the chronic gem very easy to use for time parsing and it should work for you. i tried the examples you gave.

https://github.com/mojombo/chronic