Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1

Rails parse Invalid Date Error


Im trying to figure out why I keep getting a invalid date error for certain dates.

For example:

This works:

e = "07/02/2013"
 => "07/02/2013" 
start_date = DateTime.parse(e).beginning_of_day.strftime("%Y-%d-%m %H:%M:%S")
 => "2013-07-01 00:00:00"
end_date = DateTime.parse(e).end_of_day.strftime("%Y-%d-%m %H:%M:%S")
 => "2013-07-02 23:59:59" 

This returns ArgumentError: invalid date

 e = "07/18/2013"
=> "07/18/2013" 
start_date = DateTime.parse(e).beginning_of_day.strftime("%Y-%d-%m %H:%M:%S")
ArgumentError: invalid date
from (irb):53:in `parse'
end_date = DateTime.parse(e).end_of_day.strftime("%Y-%d-%m %H:%M:%S")
ArgumentError: invalid date
from (irb):55:in `parse'

I am using the same date format in both cases. What may be the cause of this/What do I need to change to fix it?


Solution

  • The format of the date is dd/mm/yyyy. You are trying to create date which is 7th day of the 18th month of 2013.

    You can use strptime if you want to customize your date format.