Skip to content

Dates and times ​

The Time namespace holds dates, clock times, zones, and spans of time. It keeps apart what other libraries mix: a moment on the time line is one type, and what a calendar or a clock shows is another. A zone is never assumed. Every value is immutable, compares by value with ==, and renders as its ISO 8601 text, with the zone in brackets on a zoned time and a zone as its name. You need no import.

Choose a type ​

Pick the type by the question the value answers:

You holdType
when something happened, such as a log timestampTime.Instant
what time it was in a placeTime.ZonedDateTime
a day on the calendar, such as a birthdayTime.PlainDate
a time on the clock, such as an alarmTime.PlainTime
a date and a time before its zone is knownTime.PlainDateTime
a time zoneTime.Zone
a span, such as 2 hours or 1 monthTime.Duration

Each renders as the text you would write for it:

ant
let stockholm = Result.unwrap(Time.zone("Europe/Stockholm"))
let moment = Time.fromEpochMillis(1792677785000)
moment                                                                  // 2026-10-22T14:03:05Z
moment.inZone(stockholm)                                                // 2026-10-22T16:03:05+02:00[Europe/Stockholm]
Time.plainDate(2026, 10, 22)                                            // 2026-10-22
Time.plainTime(7, 30)                                                   // 07:30:00
Time.plainDateTime(Time.plainDate(2026, 10, 22), Time.plainTime(7, 30)) // 2026-10-22T07:30:00
2h30m                                                                   // PT2H30M

Functions that serve several types take one of four unions: Point for all five time types, Wall for the ones with fields, Dated for the ones with a date, and Timed for the ones with a clock time.

Get the current time ​

ant
let zone = Time.localZone()
let now = Time.now().inZone(zone)
println(now.zoneOf() == zone)
println(now.toInstant().inZone(zone) == now)
text
true
true

Build values ​

A constructor panics on a value that cannot exist. A constant span of hours and smaller units is a duration literal; a span with days, weeks, months, or years, or one built from a number, comes from Time.duration:

ant
let minutes = 300
Time.plainDate(2026, 2, 28)       // 2026-02-28
Time.plainTime(9)                 // 09:00:00
Time.duration(months: 1, days: 3) // P1M3D
Time.duration(minutes: minutes)   // PT300M
Time.plainDate(2026, 2, 29)       // panic: Time.plainDate: no such date: 2026-02-29

Read and write text ​

A time value becomes text through interpolation, String.from, and Json.stringify alike. Text from outside goes through a parse function, which returns a Result. Time has no layout strings; build another form from the fields, such as "${t.day()}.${t.month()}.${t.year()}":

ant
let due = Time.plainDate(2026, 10, 22)
"due ${due}"                      // "due 2026-10-22"
Json.stringify(Map{ "due": due }) // "{\"due\":\"2026-10-22\"}"
Time.parsePlainDate("2026-10-22") // Ok{ value: 2026-10-22 }
Time.parsePlainDate("22/10/2026") // Err{ error: Error{ message: "not a date or time: \"22/10/2026\"" } }
Time.parseDuration("1h30m")       // Ok{ value: PT1H30M }

Work with zones ​

Place a wall-clock time in a zone to get a moment, then see the same moment elsewhere:

ant
let stockholm = Result.unwrap(Time.zone("Europe/Stockholm"))
let tokyo = Result.unwrap(Time.zone("Asia/Tokyo"))
let call = Time.plainDateTime(Time.plainDate(2026, 10, 22), Time.plainTime(9)).toZoned(stockholm)
call                 // 2026-10-22T09:00:00+02:00[Europe/Stockholm]
call.withZone(tokyo) // 2026-10-22T16:00:00+09:00[Asia/Tokyo]
call.offset()        // "+02:00"

A wall-clock time in a daylight-saving gap does not exist, and toZoned moves it forward unless disambiguation: says otherwise. A program that must refuse such a time checks possibleInstants:

ant
let ny = Result.unwrap(Time.zone("America/New_York"))
let gap = Time.plainDateTime(Time.plainDate(2026, 3, 8), Time.plainTime(2, 30))
gap.toZoned(ny)                                 // 2026-03-08T03:30:00-04:00[America/New_York]
gap.toZoned(ny, disambiguation: Time.Earlier{}) // 2026-03-08T01:30:00-05:00[America/New_York]
gap.possibleInstants(ny).length()               // 0

Read and change fields ​

ant
let day = Time.plainDate(2026, 10, 22)
day.year()                  // 2026
day.dayOfWeek()             // 4
day.daysInMonth()           // 31
day.with(day: 1)            // 2026-10-01
day.with(month: 4, day: 31) // 2026-04-30

Calculate with dates and times ​

Years, months, weeks, and days move the calendar; hours and smaller units move the moment on the time line, whatever the clock shows. A month added to January 31 lands on the last day of February:

ant
let start = Time.plainDate(2026, 1, 31)
start.add(Time.duration(months: 1))                        // 2026-02-28
start.until(Time.plainDate(2026, 12, 25))                  // P328D
Time.plainTime(14, 37).round(Time.Minute{}, increment: 15) // 14:30:00
Time.compare(start, Time.plainDate(2026, 3, 1))            // -1
  • Time.add: a value moved forward by a duration
  • Time.subtract: a value moved back by a duration
  • Time.until: the duration from one value to another, negative when the other is earlier
  • Time.since: the duration from an earlier value to this one
  • Time.round: a value rounded to a unit
  • Time.compare: the order of two values, since < does not apply to them

Work with durations ​

A duration keeps its fields as written, so 90m and 1h30m are not ==. Compare and convert them by length:

ant
Time.compareDuration(90m, 1h30m)                  // 0
Time.total(1h30m, Time.Minute{})                  // 90.0
Time.roundDuration(90m, largestUnit: Time.Hour{}) // PT1H30M

A month or a year has no fixed length, so give a relativeTo: date to count from:

ant
let month = Time.duration(months: 1)
Time.total(month, Time.Day{}, relativeTo: Time.plainDate(2026, 2, 1)) // 28.0
Time.total(month, Time.Day{})                                         // panic: Time.total: a total with months needs a relativeTo point

Measure, wait, and exchange timestamps ​

Time.now reads the wall clock, which the host can set back. Measure elapsed time with Time.monotonic, which only moves forward:

ant
let start = Time.monotonic()
Time.sleep(20ms)
println(Time.monotonic() - start >= 20)
text
true

See also ​

  • Time: the namespace, with every type and function
  • Duration literals: 500ms, 2h30m, and their rules
  • Context: deadlines and timeouts, given as a Time.Duration