On this page:
Date
Date.year
Date.month
Date.day
Date.week_  day
Date.year_  day
Date.now
Date.from_  seconds
Date.to_  string
Date.to_  datetime
Date.to_  seconds

14.7.2 Date🔗ℹ

class

class date.Date(

  ~year: year :: Int,

  ~month: month :: Int.in(1 ..= 12) = 1,

  ~day: day :: Int.in(1 ..= date.days_in_month(year, month)) = 1

)

Represents a date relative to an unspecified time zone.

The date.Date class privately implements Comparable, so date.Date instances can be compared with operators like < and >. A date.Date instance is serializable.

> def dt = date.Date(~year: 1968, ~month: 1, ~day: 24)

> dt

Date(~year: 1968, ~month: 1, ~day: 24)

> dt.to_string()

"1968-01-24"

> dt < date.Date(~year: 1969, ~month: 7, ~day: 20)

#true

> date.Date(~year: 1999, ~month: 2, ~day: 29)

Date: value does not satisfy annotation

  value: 29

  annotation: Int.in(1 ..= date.days_in_month(year, month))

property

property (dt :: date.Date).week_day :: Int.in(0 ..= 6)

 

property

property (dt :: date.Date).year_day :: Int.in(0 ..= 366)

Reports a day of the week or day of the year represented by dt.

> date.Date(~year: 1968, ~month: 1, ~day: 24).week_day

3

function

fun date.Date.now(~local: local :: Any = #true) :: date.Date

Uses the system clock via system.milliseconds to get the current time and using the operating system’s facilities to get a date relative to either the current time zone (when local is true) or UTC (when local is false).

function

fun date.Date.from_seconds(

  secs :: Real,

  ~local: local :: Any = #true

) :: date.Date

Uses operating system facilities to convert a number of seconds since the epoch to a date relative to either the current time zone (when local is true) or UTC (when local is false).

> date.Date.from_seconds(0, ~local: #false)

Date(~year: 1970, ~month: 1, ~day: 1)

method

method (dt :: date.Date).to_string(

  ~format: format :: date.Format = #'rfc3339

) :: String

Converts dt to a string using the format selected by format.

> def dt1 = date.Date(~year: 1968, ~month: 1, ~day: 24)

> dt1.to_string()

"1968-01-24"

> dt1.to_string(~format: #'american)

"Wednesday, January 24th, 1968"

method

method (dt :: date.Date).to_datetime(

  ~time: tm :: date.Time = date.Time()

) :: date.DateTime

Converts a date.Date to a date.DateTime using tm for the time portion of the new date.DateTime, and using UTC as the time zone.

method

method (dt :: date.Date).to_seconds() :: Real

Equivalent to dt.to_datetime().to_seconds().