On this page:
Date  Time
Date  Time.year
Date  Time.month
Date  Time.day
Date  Time.hour
Date  Time.minute
Date  Time.second
Date  Time.nanosecond
Date  Time.week_  day
Date  Time.year_  day
Date  Time.now
Date  Time.from_  seconds
Date  Time.to_  string
Date  Time.to_  zoned
Date  Time.to_  seconds

14.7.3 Date and Time Combined🔗ℹ

class

class date.DateTime(

  ~year: year :: Int,

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

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

  ~hour: hour :: Int.in(0 ..= 23) = 0,

  ~minute: minute :: Int.in(0 ..= 59) = 0,

  ~second: second :: Int.in(0 ..= 59) = 0,

  ~nanosecond: nanosecond :: Int.in(0, 999_999_000) = 0

)

Combines the fields of date.Date and date.Time to represent a specific date and time relative to an unspecified time zone.

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

> def dt = date.DateTime(~year: 2025, ~month: 11, ~day: 14,

                         ~hour: 18, ~minute: 14)

> dt

DateTime(

  ~year: 2025,

  ~month: 11,

  ~day: 14,

  ~hour: 18,

  ~minute: 14,

  ~second: 0,

  ~nanosecond: 0

)

> dt.to_string()

"2025-11-14 18:14:00"

> dt > date.DateTime.from_seconds(0)

#true

property

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

 

property

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

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

> date.DateTime(~year: 2025, ~month: 11, ~day: 14).week_day

5

function

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

Like date.Date.now, but preserving the current time within the current day.

function

fun date.DateTime.from_seconds(

  secs :: Real,

  ~local: local :: Any = #true

) :: date.TimeDate

Like date.Date.now, but preserving the time of secs within the day of secs.

If the operating system reports a time within a leap second, then the reported time is the end of the preceding second.

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

DateTime(

  ~year: 1970,

  ~month: 1,

  ~day: 1,

  ~hour: 0,

  ~minute: 0,

  ~second: 0,

  ~nanosecond: 0

)

method

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

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

  ~time_format: time_format :: maybe(date.TimeFormat) = #'seconds,

) :: String

Converts dt to a string using the format selected by format. If time_format is not #false, then the string includes the time portion of dt. The use of time_format may be limited by format. If a format requires a time zone, then UTC is used.

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

> dt1.to_string()

"1968-01-24 00:00:10"

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

"Wed, 24 Jan 1968 00:00:10 +0000"

method

method (dt :: date.DateTime).to_zoned() :: date.ZonedDateTime

Converts a date.DateTime to a date.ZonedDateTime using using UTC as the time zone. This conversion can fail if the date falls outside the (large) range of dates that the operating system can locate.

method

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

Equivalent to dt.to_zoned().to_seconds().