Skip to content
On this page
class · Ruby 4.0

Date

< Object include Comparable 68 instance methods 33 class methods

Class Date provides methods for storing and manipulating calendar dates.

Consider using class Time instead of class Date if:

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:

Example
Date.today # => #<Date: 1999-12-31>

You can create a specific date from various combinations of arguments:

  • Date.new takes integer year, month, and day-of-month:

    Example
    Date.new(1999, 12, 31) # => #<Date: 1999-12-31>
    
  • Date.ordinal takes integer year and day-of-year:

    Example
    Date.ordinal(1999, 365) # => #<Date: 1999-12-31>
    
  • Date.jd takes integer Julian day:

    Example
    Date.jd(2451544) # => #<Date: 1999-12-31>
    
  • Date.commercial takes integer commercial data (year, week, day-of-week):

    Example
    Date.commercial(1999, 52, 5) # => #<Date: 1999-12-31>
    
  • Date.parse takes a string, which it parses heuristically:

    Example
    Date.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.strptime takes a date string and a format string, then parses the date string according to the format string:

    Example
    Date.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 ArgumentError if the string length is greater than limit.

  • Other numeric or nil: ignores limit.

  • Other non-numeric: raises TypeError.

Class methods 33

Instance methods 68

Constants

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.

No documentation available