Class Date
In: lib/date/format.rb
lib/date.rb
lib/yaml/rubytypes.rb
Parent: Object

Class representing a date.

See the documentation to the file date.rb for an overview.

Internally, the date is represented as an Astronomical Julian Day Number, ajd. The Day of Calendar Reform, sg, is also stored, for conversions to other date formats. (There is also an of field for a time zone offset, but this is only for the use of the DateTime subclass.)

A new Date object is created using one of the object creation class methods named after the corresponding date format, and the arguments appropriate to that date format; for instance, Date::civil() (aliased to Date::new()) with year, month, and day-of-month, or Date::ordinal() with year and day-of-year. All of these object creation class methods also take the Day of Calendar Reform as an optional argument.

Date objects are immutable once created.

Once a Date has been created, date values can be retrieved for the different date formats supported using instance methods. For instance, mon() gives the Civil month, cwday() gives the Commercial day of the week, and yday() gives the Ordinal day of the year. Date values can be retrieved in any format, regardless of what format was used to create the Date instance.

The Date class includes the Comparable module, allowing date objects to be compared and sorted, ranges of dates to be created, and so forth.

Methods

+   -   <<   <=>   ===   >>   _parse   _strptime   _valid_jd?   ajd   ajd_to_amjd   ajd_to_jd   amjd   amjd_to_ajd   asctime   civil   commercial   ctime   cwday   cweek   cwyear   day   day_fraction   downto   england   eql?   gregorian   gregorian?   gregorian_leap?   hash   httpdate   inspect   iso8601   italy   jd   jd   jd_to_ajd   jd_to_ld   jd_to_mjd   jd_to_wday   jisx0301   julian   julian?   julian_leap?   ld   ld_to_jd   leap?   marshal_dump   marshal_load   mday   mjd   mjd_to_jd   mon   month   new   new_start   next   next_day   next_month   next_year   ordinal   parse   prev_day   prev_month   prev_year   rfc2822   rfc3339   rfc822   rfc822   start   step   strftime   strptime   succ   time_to_day_fraction   time_to_day_fraction   to_date   to_datetime   to_s   to_time   to_yaml   today   upto   valid_civil?   valid_commercial?   valid_jd?   valid_ordinal?   wday   xmlschema   yday   year  

Included Modules

Comparable

Constants

MONTHNAMES = [nil] + %w(January February March April May June July August September October November December)   Full month names, in English. Months count from 1 to 12; a month‘s numerical representation indexed into this array gives the name of that month (hence the first element is nil).
DAYNAMES = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)   Full names of days of the week, in English. Days of the week count from 0 to 6 (except in the commercial week); a day‘s numerical representation indexed into this array gives the name of that day.
ABBR_MONTHNAMES = [nil] + %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)   Abbreviated month names, in English.
ABBR_DAYNAMES = %w(Sun Mon Tue Wed Thu Fri Sat)   Abbreviated day names, in English.
ITALY = 2299161   The Julian Day Number of the Day of Calendar Reform for Italy and the Catholic countries.
ENGLAND = 2361222   The Julian Day Number of the Day of Calendar Reform for England and her Colonies.
JULIAN = Infinity.new   A constant used to indicate that a Date should always use the Julian calendar.
GREGORIAN = -Infinity.new   A constant used to indicate that a Date should always use the Gregorian calendar.

External Aliases

_rfc2822 -> _rfc822
gregorian_leap? -> leap?
new -> new!
valid_civil? -> valid_date?
civil -> new

Public Class methods

Create a new Date object for the Civil Date specified by year y, month m, and day-of-month d.

m and d can be negative, in which case they count backwards from the end of the year and the end of the month respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised. can be negative

y defaults to -4712, m to 1, and d to 1; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

Create a new Date object for the Commercial Date specified by year y, week-of-year w, and day-of-week d.

Monday is day-of-week 1; Sunday is day-of-week 7.

w and d can be negative, in which case they count backwards from the end of the year and the end of the week respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised.

y defaults to -4712, w to 1, and d to 1; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

Is a year a leap year in the Gregorian calendar?

All years divisible by 4 are leap years in the Gregorian calendar, except for years divisible by 100 and not by 400.

Create a new Date object from a Julian Day Number.

jd is the Julian Day Number; if not specified, it defaults to 0. sg specifies the Day of Calendar Reform.

Is a year a leap year in the Julian calendar?

All years divisible by 4 are leap years in the Julian calendar.

NOTE this is the documentation for the method new!(). If you are reading this as the documentation for new(), that is because rdoc doesn‘t fully support the aliasing of the initialize() method. new() is in fact an alias for civil(): read the documentation for that method instead.

Create a new Date object.

ajd is the Astronomical Julian Day Number. of is the offset from UTC as a fraction of a day. Both default to 0.

sg specifies the Day of Calendar Reform to use for this Date object.

Using one of the factory methods such as Date::civil is generally easier and safer.

Create a new Date object from an Ordinal Date, specified by year y and day-of-year d. d can be negative, in which it counts backwards from the end of the year. No year wraparound is performed, however. An invalid value for d results in an ArgumentError being raised.

y defaults to -4712, and d to 1; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

Create a new Date object by parsing from a String, without specifying the format.

str is a String holding a date representation. comp specifies whether to interpret 2-digit years as 19XX (>= 69) or 20XX (< 69); the default is not to. The method will attempt to parse a date from the String using various heuristics; see _parse in date/format.rb for more details. If parsing fails, an ArgumentError will be raised.

The default str is ’-4712-01-01’; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

Create a new Date object by parsing from a String according to a specified format.

str is a String holding a date representation. fmt is the format that the date is in. See date/format.rb for details on supported formats.

The default str is ’-4712-01-01’, and the default fmt is ’%F’, which means Year-Month-Day_of_Month. This gives Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

An ArgumentError will be raised if str cannot be parsed.

Create a new Date object representing today.

sg specifies the Day of Calendar Reform.

Public Instance methods

Return a new Date object that is n days later than the current one.

n may be a negative value, in which case the new Date is earlier than the current one; however, #-() might be more intuitive.

If n is not a Numeric, a TypeError will be thrown. In particular, two Dates cannot be added to each other.

If x is a Numeric value, create a new Date object that is x days earlier than the current one.

If x is a Date, return the number of days between the two dates; or, more precisely, how many days later the current date is than x.

If x is neither Numeric nor a Date, a TypeError is raised.

Return a new Date object that is n months earlier than the current one.

If the day-of-the-month of the current Date is greater than the last day of the target month, the day-of-the-month of the returned Date will be the last day of the target month.

Compare this date with another date.

other can also be a Numeric value, in which case it is interpreted as an Astronomical Julian Day Number.

Comparison is by Astronomical Julian Day Number, including fractional days. This means that both the time and the timezone offset are taken into account when comparing two DateTime instances. When comparing a DateTime instance with a Date instance, the time of the latter will be considered as falling on midnight UTC.

The relationship operator for Date.

Compares dates by Julian Day Number. When comparing two DateTime instances, or a DateTime with a Date, the instances will be regarded as equivalent if they fall on the same date in local time.

Return a new Date object that is n months later than the current one.

If the day-of-the-month of the current Date is greater than the last day of the target month, the day-of-the-month of the returned Date will be the last day of the target month.

Is jd a valid Julian Day Number?

If it is, returns it. In fact, any value is treated as a valid Julian Day Number.

Get the date as an Astronomical Julian Day Number.

Convert an Astronomical Julian Day Number to an Astronomical Modified Julian Day Number.

Convert an Astronomical Julian Day Number to a (civil) Julian Day Number.

ajd is the Astronomical Julian Day Number to convert. of is the offset from UTC as a fraction of a day (defaults to 0).

Returns the (civil) Julian Day Number as [day_number, fraction] where fraction is always 1/2.

Get the date as an Astronomical Modified Julian Day Number.

Convert an Astronomical Modified Julian Day Number to an Astronomical Julian Day Number.

alias_method :format, :strftime

ctime()

Alias for asctime

Get the commercial day of the week of this date. Monday is commercial day-of-week 1; Sunday is commercial day-of-week 7.

Get the commercial week of the year of this date.

Get the commercial year of this date. See Commercial Date in the introduction for how this differs from the normal year.

day()

Alias for mday

Get any fractional day part of the date.

Step backward one day at a time until we reach min (inclusive), yielding each date as we go.

Create a copy of this Date object that uses the English/Colonial Day of Calendar Reform.

Is this Date equal to other?

other must both be a Date object, and represent the same date.

Create a copy of this Date object that always uses the Gregorian Calendar.

Is the current date new-style (Gregorian Calendar)?

Calculate a hash value for this date.

Return internal object state as a programmer-readable string.

Create a copy of this Date object that uses the Italian/Catholic Day of Calendar Reform.

Get the date as a Julian Day Number.

Convert a (civil) Julian Day Number to an Astronomical Julian Day Number.

jd is the Julian Day Number to convert, and fr is a fractional day. of is the offset from UTC as a fraction of a day (defaults to 0).

Returns the Astronomical Julian Day Number as a single numeric value.

Convert a Julian Day Number to the number of days since the adoption of the Gregorian Calendar (in Italy).

Convert a Julian Day Number to a Modified Julian Day Number.

Convert a Julian Day Number to the day of the week.

Sunday is day-of-week 0; Saturday is day-of-week 6.

Create a copy of this Date object that always uses the Julian Calendar.

Is the current date old-style (Julian Calendar)?

Get the date as the number of days since the Day of Calendar Reform (in Italy and the Catholic countries).

Convert a count of the number of days since the adoption of the Gregorian Calendar (in Italy) to a Julian Day Number.

Is this a leap year?

Dump to Marshal format.

Load from Marshal format.

Get the day-of-the-month of this date.

Get the date as a Modified Julian Day Number.

Convert a Modified Julian Day Number to a Julian Day Number.

Get the month of this date.

January is month 1.

month()

Alias for mon

Create a copy of this Date object using a new Day of Calendar Reform.

Return a new Date one day after this one.

rfc822()

Alias for rfc2822

rfc822()

Alias for rfc2822

When is the Day of Calendar Reform for this Date object?

Step the current date forward step days at a time (or backward, if step is negative) until we reach limit (inclusive), yielding the resultant date at each step.

succ()

Alias for next

Return the date as a human-readable string.

The format used is YYYY-MM-DD.

Step forward one day at a time until we reach max (inclusive), yielding each date as we go.

Get the week day of this date. Sunday is day-of-week 0; Saturday is day-of-week 6.

Get the day-of-the-year of this date.

January 1 is day-of-the-year 1

Get the year of this date.

[Validate]

ruby-doc.org is hosted and maintained by James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, Arizona. The site was created in 2002 as part of the Ruby Documentation Project to promote the Ruby language and to help other Ruby hackers.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.