Class

Date

Extends:

Includes:

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.

Modules
Format
Classes
Infinity
Constants
ABBR_DAYNAMES Abbreviated day names, in English.
ABBR_MONTHNAMES Abbreviated month names, in English.
DAYNAMES 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.
ENGLAND The Julian Day Number of the Day of Calendar Reform for England and her Colonies.
GREGORIAN A constant used to indicate that a Date should always use the Gregorian calendar.
ITALY The Julian Day Number of the Day of Calendar Reform for Italy and the Catholic countries.
JULIAN A constant used to indicate that a Date should always use the Julian calendar.
MONTHNAMES 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).
UNIXEPOCH
Aliases
leap?
new
new!
valid_date?
Public Methods
+ Return a new Date object that is n days later than the current one.
- If x is a Numeric value, create a new Date object that is x days earlier than the current one.
<< Return a new Date object that is n months earlier than the current one.
<=> Compare this date with another date.
=== The relationship operator for Date.
>> Return a new Date object that is n months later than the current one.
_dump Dump to Marshal format.
_load Load from Marshall format.
_parse
_strptime
ajd Get the date as an Astronomical Julian Day Number.
ajd_to_amjd Convert an Astronomical Julian Day Number to an Astronomical Modified Julian Day Number.
ajd_to_jd Convert an Astronomical Julian Day Number to a (civil) Julian Day Number.
amjd Get the date as an Astronomical Modified Julian Day Number.
amjd_to_ajd Convert an Astronomical Modified Julian Day Number to an Astronomical Julian Day Number.
asctime alias_method :format, :strftime
civil Create a new Date object for the Civil Date specified by year y, month m, and day-of-month d.
civil_to_jd Convert a Civil Date to a Julian Day Number. y, m, and d are the year, month, and day of the month. sg specifies the Day of Calendar Reform.
commercial Create a new Date object for the Commercial Date specified by year y, week-of-year w, and day-of-week d.
commercial_to_jd Convert a Commercial Date to a Julian Day Number.
ctime Alias for #asctime
cwday Get the commercial day of the week of this date. Monday is commercial day-of-week 1; Sunday is commercial day-of-week 7.
cweek Get the commercial week of the year of this date.
cwyear 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
day_fraction Get any fractional day part of the date.
day_fraction_to_time Convert a fractional day fr to [hours, minutes, seconds, fraction_of_a_second]
downto Step backward one day at a time until we reach min (inclusive), yielding each date as we go.
england Create a copy of this Date object that uses the English/Colonial Day of Calendar Reform.
eql? Is this Date equal to other?
gregorian Create a copy of this Date object that always uses the Gregorian Calendar.
gregorian? Is the current date new-style (Gregorian Calendar)?
gregorian? Does a given Julian Day Number fall inside the new-style (Gregorian) calendar?
gregorian_leap? Is a year a leap year in the Gregorian calendar?
hash Calculate a hash value for this date.
inspect Return internal object state as a programmer-readable string.
italy Create a copy of this Date object that uses the Italian/Catholic Day of Calendar Reform.
jd Get the date as a Julian Day Number.
jd Create a new Date object from a Julian Day Number.
jd_to_ajd Convert a (civil) Julian Day Number to an Astronomical Julian Day Number.
jd_to_civil Convert a Julian Day Number to a Civil Date. jd is the Julian Day Number. sg specifies the Day of Calendar Reform.
jd_to_commercial Convert a Julian Day Number to a Commercial Date
jd_to_ld Convert a Julian Day Number to the number of days since the adoption of the Gregorian Calendar (in Italy).
jd_to_mjd Convert a Julian Day Number to a Modified Julian Day Number.
jd_to_ordinal Convert a Julian Day Number to an Ordinal Date.
jd_to_wday Convert a Julian Day Number to the day of the week.
julian Create a copy of this Date object that always uses the Julian Calendar.
julian? Is the current date old-style (Julian Calendar)?
julian? Does a given Julian Day Number fall inside the old-style (Julian) calendar?
julian_leap? Is a year a leap year in the Julian calendar?
ld Get the date as the number of days since the Day of Calendar Reform (in Italy and the Catholic countries).
ld_to_jd Convert a count of the number of days since the adoption of the Gregorian Calendar (in Italy) to a Julian Day Number.
leap? Is this a leap year?
mday Get the day-of-the-month of this date.
mjd Get the date as a Modified Julian Day Number.
mjd_to_jd Convert a Modified Julian Day Number to a Julian Day Number.
mon Get the month of this date.
month Alias for #mon
new 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.
new_start Create a copy of this Date object using a new Day of Calendar Reform.
next Return a new Date one day after this one.
ordinal 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.
ordinal_to_jd Convert an Ordinal Date to a Julian Day Number.
parse Create a new Date object by parsing from a String, without specifying the format.
start When is the Day of Calendar Reform for this Date object?
step 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.
strftime
strptime Create a new Date object by parsing from a String according to a specified format.
succ Alias for #next
time_to_day_fraction Convert an h hour, min minutes, s seconds period to a fractional day.
to_s Return the date as a human-readable string.
to_yaml
today Create a new Date object representing today.
upto Step forward one day at a time until we reach max (inclusive), yielding each date as we go.
valid_civil? Do year y, month m, and day-of-month d make a valid Civil Date? Returns the corresponding Julian Day Number if they do, nil if they don’t.
valid_commercial? Do year y, week-of-year w, and day-of-week d make a valid Commercial Date? Returns the corresponding Julian Day Number if they do, nil if they don’t.
valid_jd? Is jd a valid Julian Day Number?
valid_ordinal? Do the year y and day-of-year d make a valid Ordinal Date? Returns the corresponding Julian Day Number if they do, or nil if they don’t.
valid_time? Do hour h, minute min, and second s constitute a valid time?
wday Get the week day of this date. Sunday is day-of-week 0; Saturday is day-of-week 6.
yday Get the day-of-the-year of this date.
year Get the year of this date.
zone_to_diff
Private Methods
_parse_beat
_parse_day
_parse_ddd
_parse_eu
_parse_iso
_parse_iso2
_parse_jis
_parse_mday
_parse_mon
_parse_sla_eu
_parse_sla_ja
_parse_sla_us
_parse_time
_parse_us
_parse_vms
_parse_year
_strptime_i
civil Get the date as a Civil Date, [year, month, day_of_month]
commercial Get the date as a Commercial Date, [year, week_of_year, day_of_week]
complete_frags
emit
emit_a
emit_ad
emit_au
emit_n
emit_sn
emit_w
emit_z
fix_style
fix_style
hour Get the hour of this date.
jd_to_weeknum
min Get the minute of this date.
new_by_frags
new_offset
next_day
now Create a new DateTime object representing the current time.
num_pattern?
offset
once
ordinal Get the date as an Ordinal Date, [year, day_of_year]
rewrite_frags
s3e
sec Get the second of this date.
sec_fraction Get the fraction-of-a-second of this date. The unit is in days. I do NOT recommend you to use this method.
time Get the time of this date as [hours, minutes, seconds, fraction_of_a_second]
valid_date_frags?
valid_time_frags?
valid_weeknum?
weeknum
weeknum0
weeknum1
weeknum_to_jd
wnum0
wnum1
zone
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.