Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 244
Yogzzz Avatar asked Oct 03 '22 11:10

Yogzzz


1 Answers

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.

like image 94
Ermin Dedovic Avatar answered Oct 13 '22 11:10

Ermin Dedovic