method

to_date

v3.0.9 - Show latest stable - Class: String
to_date()
public

No documentation available.

# File activesupport/lib/active_support/core_ext/string/conversions.rb, line 42
  def to_date
    return nil if self.blank?
    ::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
  end

2Notes

Month-first date string no longer parses correctly

lulalala · Jun 15, 20122 thanks

The following date format won't be parsed correctly:

'06/15/2008'.to_date 

Use this instead:

Date.strptime("6/15/2012", '%m/%d/%Y')

String to date conversion not necessarily symmetric

MikeBlyth · Nov 28, 2012

Note that converting from Date to String and back again is not necessarily symmetric, because the conversion to string may result in a format that is not properly converted by to_date.

For one thing, to_date sets the century argument in _parse to false. Thus if the default date format has a two-digit year, like the :short one, the century will be dropped.

Date.today.to_s.to_date #=> Mon, 28 Nov 0012