Ruby 1.87 vs 1.92 Date.parse
Use strptime
and give a specific time format.
ruby-1.9.2-p136 :022 > Date.strptime '03/21/2011', '%m/%d/%Y'
=> #<Date: 2011-03-21 (4911283/2,0,2299161)>
See michaelmichael's response for the reason for this difference between Ruby versions.
How do I change the format ActiveRecord expects when parsing dates from a text field in a form?
I initially thought this could be solved through the Rails internationalization features, but it turns out I was wrong.
Ever since Ruby 1.9, the standard format for date parsing is dd/mm/yyyy
, so as to better accomodate international users. More details can be found in this SO answer.
That standard is maintained in Rails, as Date.parse
is now used to process data from form inputs. Using a before_validation
callback won't work because the field is going to be received as nil
by the callback method.
Right now there are two gems dealing with this specific issue, namely that date parsing in Rails does not follow the locale settings from I18n.locale
. Both seem to work well.
delocalize, by clemens - Seems to have been applied successfully in a decent number or projects and has the highest number of stars at the moment.
i18n_alchemy by carlosantoniodasilva - This one has been released more recently. The author is a Rails core team member, and a very active one at that. Definitely deserves a look.
Rails: Date.parse vs string.to_date
If you check the code here, you'll see that the second way of doing this really just invokes the first method with only a minor check for blank-ness.
def to_date
::Date.parse(self, false) unless blank?
end
Regarding the false
argument, consulting the docs here:
If the optional second argument is true and the detected year is in the range “00” to “99”, considers the year a 2-digit form and makes it full.
Which should I always parse date times with? DateTime, Time, Time.zone?
Go with Time.zone.parse if you just want to write into ActiveRecord.
DateTime should be avoided. If you're handling dates, you should use Date.parse instead.
Beyond that, it depends on whether the input comes with timezone information, what the current timezone is set to, and whether you want timezones in your data.
Time.zone.parse will return an ActiveSupport::TimeWithZone, defaulting to UTC.
> Time.zone.parse("12:30")
=> Thu, 10 May 2012 12:30:00 UTC +00:00
Time.parse will return a Time, with a zone if it's specified in the input, or the local TZ.
> Time.parse("12:30")
=> 2012-05-09 12:30:00 -0700
For a more detailed explanation of Ruby time comparisons and precision, read this blog post:
http://blog.solanolabs.com/rails-time-comparisons-devil-details-etc/
Related Topics
What Are the Things You Would Like Improved in the Ruby Language
Help Refactoring This Nasty Ruby If/Else Statement
Rails: How to Autocomplete Search for Name But Save Id
Rails 3.2.2 (Or 3.2.1) + Postgresql 9.1.3 + Ubuntu 11.10 Connection Error
Gem File with Git Remote Failing on Heroku Push
Group_By in Rails by 2 or More Attributes
How to Delete Special Characters
Error Message "Xcode Alone Is Not Sufficient on Sierra"
How to Validate the Date Such That It Is After Today in Rails
Why Is This String Key in a Hash Converted to a Symbol
How to Return a Value from a Thread in Ruby
Build Hash from Collection of Activerecord Models
How to Get Files Count in a Directory Using Ruby
Ruby, Check If Date Is a Weekend
How to Replace Multiple Newlines in a Row with One Newline Using Ruby
What Is the Preferred Way (Better Style) to Name a Namespace in Ruby? Singular or Plural