method

to_date

rails latest stable - Class: ActiveSupport::CoreExtensions::String::Conversions

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

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

mutru · Jul 13, 20085 thanks

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

LacKac · Jul 24, 20082 thanks

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