v4.2.7 - Show latest stable - 2 notes - Class: String
to_date() public

Converts a string to a Date value.

"1-1-2012".to_date   # => Sun, 01 Jan 2012
"01/01/2012".to_date # => Sun, 01 Jan 2012
"2012-12-13".to_date # => Thu, 13 Dec 2012
"12/13/2012".to_date # => ArgumentError: invalid date
Show source
Register or log in to add new notes.
June 15, 2012 - (<= v3.2.3)
1 thank

Month-first date string no longer parses correctly

The following date format won’t be parsed correctly:


Use this instead:

Date.strptime("6/15/2012", '%m/%d/%Y')
November 28, 2012 - (>= v3.0.0)
0 thanks

String to date conversion not necessarily symmetric

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