Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Date.strptime

Date.strptime(string = '-4712-01-01', format = '%F', start = Date::ITALY) → date

Returns a new Date object with values parsed from string, according to the given format:

Example
Date.strptime('2001-02-03', '%Y-%m-%d')  # => #<Date: 2001-02-03>
Date.strptime('03-02-2001', '%d-%m-%Y')  # => #<Date: 2001-02-03>
Date.strptime('2001-034', '%Y-%j')       # => #<Date: 2001-02-03>
Date.strptime('2001-W05-6', '%G-W%V-%u') # => #<Date: 2001-02-03>
Date.strptime('2001 04 6', '%Y %U %w')   # => #<Date: 2001-02-03>
Date.strptime('2001 05 6', '%Y %W %u')   # => #<Date: 2001-02-03>
Date.strptime('sat3feb01', '%a%d%b%y')   # => #<Date: 2001-02-03>

For other formats, see Formats for Dates and Times. (Unlike Date.strftime, does not support flags and width.)

See argument start.

See also strptime(3).

Related: Date._strptime (returns a hash).

See also ._strptime .today .rfc822 .valid_civil? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0