On this page
Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
Date
Class Date provides methods for storing and manipulating calendar dates.
Consider using class Time instead of class Date if:
-
You need both dates and times; Date handles only dates.
-
You need only Gregorian dates (and not Julian dates); see Julian and Gregorian Calendars.
A Date object, once created, is immutable, and cannot be modified.
Creating a Date
You can create a date for the current date, using Date.today:
Date.today # => #<Date: 1999-12-31>
You can create a specific date from various combinations of arguments:
-
Date.newtakes integer year, month, and day-of-month:ExampleDate.new(1999, 12, 31) # => #<Date: 1999-12-31>
-
Date.ordinaltakes integer year and day-of-year:ExampleDate.ordinal(1999, 365) # => #<Date: 1999-12-31>
-
Date.jdtakes integer Julian day:ExampleDate.jd(2451544) # => #<Date: 1999-12-31>
-
Date.commercialtakes integer commercial data (year, week, day-of-week):ExampleDate.commercial(1999, 52, 5) # => #<Date: 1999-12-31>
-
Date.parsetakes a string, which it parses heuristically:ExampleDate.parse('1999-12-31') # => #<Date: 1999-12-31> Date.parse('31-12-1999') # => #<Date: 1999-12-31> Date.parse('1999-365') # => #<Date: 1999-12-31> Date.parse('1999-W52-5') # => #<Date: 1999-12-31>
-
Date.strptimetakes a date string and a format string, then parses the date string according to the format string:ExampleDate.strptime('1999-12-31', '%Y-%m-%d') # => #<Date: 1999-12-31> Date.strptime('31-12-1999', '%d-%m-%Y') # => #<Date: 1999-12-31> Date.strptime('1999-365', '%Y-%j') # => #<Date: 1999-12-31> Date.strptime('1999-W52-5', '%G-W%V-%u') # => #<Date: 1999-12-31> Date.strptime('1999 52 5', '%Y %U %w') # => #<Date: 1999-12-31> Date.strptime('1999 52 5', '%Y %W %u') # => #<Date: 1999-12-31> Date.strptime('fri31dec99', '%a%d%b%y') # => #<Date: 1999-12-31>
See also the specialized methods in “Specialized Format Strings” in Formats for Dates and Times
Argument limit
Certain singleton methods in Date that parse string arguments also take optional keyword argument limit, which can limit the length of the string argument.
When limit is:
-
Non-negative: raises
ArgumentErrorif the string length is greater than limit. -
Other numeric or
nil: ignoreslimit. -
Other non-numeric: raises
TypeError.
Class methods 33
Instance methods 68
An array of strings of abbreviated day names in English. The first is “Sun”.
An array of strings of abbreviated month names in English. The first element is nil.
An array of strings of the full names of days of the week in English. The first is “Sunday”.
The Julian day number of the day of calendar reform for England and her colonies.
The Julian day number of the day of calendar reform for the proleptic Gregorian calendar.
The Julian day number of the day of calendar reform for Italy and some catholic countries.
The Julian day number of the day of calendar reform for the proleptic Julian calendar.
An array of strings of full month names in English. The first element is nil.