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

ValueError if 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

DataError if the input is not a valid YYYY-MM-DD date.

today() -> Date

Returns the current UTC date.

Fields

day @ Int [get]

Day of month.

month @ Month [get]

Calendar month.

weekday @ Weekday [get]

Day of week.

year @ Int [get]

Calendar year.

Methods

(eq) other -> Bool

Compares two dates.

Parameters

NameTypeDescription
other Value

(lt) other -> Bool

Orders two dates.

Parameters

NameTypeDescription
other Date

(str)() -> 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

OverflowError if 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

OverflowError if the result is outside the supported range.