Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

Date.parse

(?::String str, ?::boolish complete, ?::Integer start) -> ::Date
Date.parse(string = '-4712-01-01', comp = true, start = Date::ITALY, limit: 128) → date

Note: This method recognizes many forms in string, but it is not a validator. For formats, see “Specialized Format Strings” in Formats for Dates and Times If string does not specify a valid date, the result is unpredictable; consider using Date._strptime instead.

Returns a new Date object with values parsed from string:

Example
Date.parse('2001-02-03')   # => #<Date: 2001-02-03>
Date.parse('20010203')     # => #<Date: 2001-02-03>
Date.parse('3rd Feb 2001') # => #<Date: 2001-02-03>

If comp is true and the given year is in the range (0..99), the current century is supplied; otherwise, the year is taken as given:

Example
Date.parse('01-02-03', true)  # => #<Date: 2001-02-03>
Date.parse('01-02-03', false) # => #<Date: 0001-02-03>

See:

Related: Date._parse (returns a hash).

See also ._parse ._rfc2822 .ordinal .rfc2822 also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0