With Ruby 1.8.7:
>> require 'time'
>> Time.parse '01/28/2012'
=> Sat Jan 28 00:00:00 +0200 2012
>> Time.parse '28/01/2012'
=> ArgumentError: argument out of range
With Ruby 1.9.3:
>> require 'time'
>> Time.parse '28/01/2012'
=> 2012-01-28 00:00:00 +0200
>> Time.parse '01/28/2012'
=> ArgumentError: argument out of range
It looks like in Ruby 1.8.7 it was accepting US format (month/day/year), while in Ruby 1.9.3 it accepts only non US format (day/month/year).
Is there a way to change this behavior to be like Ruby 1.8.7?
Would it be an option for you to use Time.strptime("01/28/2012", "%m/%d/%Y") in place of Time.parse? That way you have better control over how Ruby is going to parse the date.
If not there are gems: (e.g. ruby-american_date) to make the Ruby 1.9 Time.parse behave like Ruby 1.8.7, but only use it if it's absolutely necessary.
1.9.3-p0 :002 > Time.parse '01/28/2012'
ArgumentError: argument out of range
1.9.3-p0 :003 > require 'american_date'
1.9.3-p0 :004 > Time.parse '01/28/2012'
=> 2012-01-28 00:00:00 +0000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With