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.new

(?::Integer year, ?::Integer month, ?::Integer mday, ?::Integer start) -> ::Date
Date.new(year = -4712, month = 1, mday = 1, start = Date::ITALY) → date

Returns a new Date object constructed from the given arguments:

Example
Date.new(2022).to_s        # => "2022-01-01"
Date.new(2022, 2).to_s     # => "2022-02-01"
Date.new(2022, 2, 4).to_s  # => "2022-02-04"

Argument month should be in range (1..12) or range (-12..-1); when the argument is negative, counts backward from the end of the year:

Example
Date.new(2022, -11, 4).to_s # => "2022-02-04"

Argument mday should be in range (1..n) or range (-n..-1) where n is the number of days in the month; when the argument is negative, counts backward from the end of the month.

See argument start.

Related: Date.jd.

See also .leap? .ordinal .julian_leap? ._parse also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0