Date parse - month and day reversed

I am getting the month and day reversed in parsing a date. I am wondering if there is some intelligence in Ruby that knows that at the moment I am in Mexico, as the parsing is working as if I was

“6/15/2008”.to_date
=> ArgumentError: invalid date

“15/6/2008”.to_date
=> Sun, 15 Jun 2008

What I really want is my date to be understood as month/day/year. Is there a way to tell Ruby or Rails to do this? Been poking around on google and do not see an answer that works here.

I found a page with this example, which makes me think maybe there is something weird going on due to my geographic location:

'06/15/2008'.[to_date](http://apidock.com/rails/ActiveSupport/CoreExtensions/String/Conversions/to_date)         # => Sun, 15 Jun 2008

Ok, feel a little stupid, if I put the date in the form yyyy-mm-dd and then parse it I get the right result. Anyhow, this works as a resolution for me but is still I think an important question if in the case I was receiving dates in something like ‘mm/dd/yyyy’ how I can get it to parse right.

Ok, feel a little stupid, if I put the date in the form yyyy-mm-dd and then parse it I get the right result. Anyhow, this works as a resolution for me but is still I think an important question if in the case I was receiving dates in something like ‘mm/dd/yyyy’ how I can get it to parse right.

You should probably use something like strptime rather than rely on ruby guessing the format (which could be product of locale settings, heuristics like ‘if the first number is 15 then it can’t be the month’ and who knows what else

Fred

Might wanna look at :
https://github.com/adzap/timeliness
https://github.com/adzap/validates_timeliness

Robert Pankowecki
http://robert.pankowecki.pl

you might want to create a file: config/initializers/date_formats.rb
and add the following line to that file...
Date::DATE_FORMATS[:default] = "%m/%d/%Y"

to set a default date format

Hi,

Take a look at strftime, you can convert to any time format by doing string.to_date.strftime(desired format)

for example lets say that you have “2011/12/31” and you want to conver it to “12-31-11” you would do
“2011/12/31”.to_date…strftime("%d-%m-%Y")

Jaz