Skip to content

Date

A calendar date, with no time of day and no time zone.

To shift a date, use add_days and sub_days -- a Duration cannot be added to a Date.

Class Methods

from_ymd year month day -> Date

Builds a date from calendar components.

Parameters

NameTypeDescription
year Int Calendar year.
month (Month | Int) Month, or its number from 1 through 12.
day Int Day of month.

Errors

Exception Condition
TypeError An argument has the wrong type
ValueError The month is out of range, or the components are not a real date

Example

let d = Date.from_ymd 2024 1 2
let d = Date.from_ymd 2024 $Month.JANUARY 2

parse_rfc text -> Date

Parses an RFC full-date, YYYY-MM-DD.

Parameters

NameTypeDescription
text Str Date text.

Errors

Exception Condition
TypeError text is not a Str
ValueError The input is not a valid YYYY-MM-DD date

today() -> Date

Returns the current UTC date.

Fields

day @ Int

Day of month.

month @ Month

Calendar month.

weekday @ Weekday

Day of week.

year @ Int

Calendar year.

Methods

(eq) other

Compares two dates.

(hash)()

Hashes the value.

(lt) other

Orders two dates.

(str)()

Returns the YYYY-MM-DD form, as rfc does.

(sub) other -> Duration

Returns the Duration between the two dates' midnights.

The result is always a whole number of days.

Parameters

NameTypeDescription
other Date Date to subtract.

add_days days -> Date

Returns the date a signed number of days later.

Parameters

NameTypeDescription
days Int Days to add. May be negative.

Errors

Exception Condition
TypeError days is not an Int
OverflowError The result is outside the supported range

Example

let tomorrow = Date.today().add_days(1)

datetime() -> DateTime

Returns midnight UTC on this date.

rfc() -> Str

Returns the RFC full-date representation, YYYY-MM-DD.

sub_days days -> Date

Returns the date a signed number of days earlier.

Parameters

NameTypeDescription
days Int Days to subtract. May be negative.

Errors

Exception Condition
TypeError days is not an Int
OverflowError The result is outside the supported range