method
to_date
rails latest stable - Class:
ActiveSupport::CoreExtensions::String::Conversions
to_date()public
No documentation available.
# File activesupport/lib/active_support/core_ext/string/conversions.rb, line 18
def to_date
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
end 2Notes
Convert strings to Dates
Uses the undocumented Date._parse method. Some usage examples:
'06/15/2008'.to_date # => Sun, 15 Jun 2008
'20080615'.to_date # => Sun, 15 Jun 2008
'2008-06-15'.to_date # => Sun, 15 Jun 2008
'Sun, 15 Jun 2008'.to_date # => Sun, 15 Jun 2008
If you need something more
Use the brilliant chronic gem: http://chronic.rubyforge.org/
require 'chronic'
Time.now # => Fri Jul 25 00:00:25 0200 2008
Chronic.parse 'tomorrow 8 in the evening' # => Sat Jul 26 20:00:00 0200 2008
Chronic.parse 'next Monday noon' # => Mon Jul 28 12:00:00 0200 2008
Chronic.parse 'first Wednesday of Aug' # => Wed Aug 06 12:00:00 0200 2008
Chronic.parse 'first Wednesday of Aug 7pm' # => Wed Aug 06 19:00:00 0200 2008