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
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"