Time.compare
func Time.compare[T: Time.Point](a: T, b: T): intTime.compare returns -1, 0 or 1 as a is before, at or after b. Instants and ZonedDateTimes compare by instant, so two zoned values at one moment in different zones compare as 0 though they are not ==; the plain types compare by fields. The comparison operators do not apply to time values.
Basic examples
Compare two dates:
let spring = Time.plainDate(2026, 3, 20)
let summer = Time.plainDate(2026, 6, 21)
Time.compare(spring, summer) // -1
Time.compare(summer, spring) // 1
Time.compare(spring, spring) // 0Test whether one moment comes before another:
let sent = Time.fromEpochMillis(1_700_000_000_000)
let read = Time.fromEpochMillis(1_700_000_060_000)
Time.compare(sent, read) < 0 // trueScope
Call it through . or in a pipe:
let a = Time.plainDate(2026, 1, 15)
let b = Time.plainDate(2025, 12, 24)
a.compare(b) // 1
a | Time.compare(b) // 1Two ZonedDateTimes compare by instant. The same moment in Stockholm and in New York compares as 0, although the two values are not ==:
let moment = Time.fromEpochMillis(1_776_866_585_000)
let stockholm = moment.inZone(Time.zone("Europe/Stockholm").unwrap())
let newYork = moment.inZone(Time.zone("America/New_York").unwrap())
stockholm // 2026-04-22T16:03:05+02:00[Europe/Stockholm]
newYork // 2026-04-22T10:03:05-04:00[America/New_York]
Time.compare(stockholm, newYork) // 0
stockholm == newYork // falseUse == when the zone matters too.
The same wall-clock time in two zones is two moments. Nine in the morning comes to Tokyo before it comes to London:
let nine = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(9))
let tokyo = nine.toZoned(Time.zone("Asia/Tokyo").unwrap())
let london = nine.toZoned(Time.zone("Europe/London").unwrap())
Time.compare(tokyo, london) // -1A PlainDateTime compares by date first, then by time:
let morning = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(9))
let lateEve = Time.plainDateTime(Time.plainDate(2026, 5, 3), Time.plainTime(23, 30))
Time.compare(lateEve, morning) // -1A PlainTime compares by the clock alone, down to the nanosecond:
Time.compare(Time.plainTime(23, 59), Time.plainTime(0, 1)) // 1
Time.compare(Time.plainTime(12, nanosecond: 1), Time.plainTime(12)) // 1A value of a union such as Time.Point passes once is narrows it to one type:
func sameDay(a: Time.Point, b: Time.PlainDate): bool {
a is Time.PlainDate && Time.compare(a, b) == 0
}
sameDay(Time.plainDate(2026, 5, 4), Time.plainDate(2026, 5, 4)) // true
sameDay(Time.plainTime(9), Time.plainDate(2026, 5, 4)) // falseApplications
Time.compare has the shape of a comparator, so it sorts an array of time values directly:
let dates = [Time.plainDate(2026, 3, 1), Time.plainDate(2025, 12, 24), Time.plainDate(2026, 1, 15)]
dates.sortWith(Time.compare) // [2025-12-24, 2026-01-15, 2026-03-01]Sort records by a time field, newest first, by swapping the arguments:
type Post = { title: string, published: Time.PlainDate }
let posts = [
Post{ title: "Hello", published: Time.plainDate(2025, 11, 2) },
Post{ title: "Release", published: Time.plainDate(2026, 4, 9) },
Post{ title: "Roadmap", published: Time.plainDate(2026, 1, 20) },
]
let newestFirst = func(a: Post, b: Post): int { Time.compare(b.published, a.published) }
for post in posts.sortWith(newestFirst) {
println("${post.published} ${post.title}")
}2026-04-09 Release
2026-01-20 Roadmap
2025-11-02 HelloCheck that a date falls in a range, both ends included:
func within(d: Time.PlainDate, start: Time.PlainDate, end: Time.PlainDate): bool {
Time.compare(d, start) >= 0 && Time.compare(d, end) <= 0
}
let start = Time.plainDate(2026, 6, 1)
let end = Time.plainDate(2026, 8, 31)
within(Time.plainDate(2026, 7, 14), start, end) // true
within(Time.plainDate(2026, 9, 1), start, end) // falseFind the latest of several moments with Array.reduce:
let logins = [
Time.fromEpochMillis(1_776_000_000_000),
Time.fromEpochMillis(1_776_900_000_000),
Time.fromEpochMillis(1_776_500_000_000),
]
logins.reduce(logins[0], func(latest: Time.Instant, t: Time.Instant) {
if Time.compare(t, latest) > 0 { t } else { latest }
}) // 2026-04-22T23:20:00ZTell whether a deadline has passed. The program fixes now so that it prints the same every run; a real one calls Time.now():
let deadline = Time.parseZoned("2026-05-01T17:00:00+02:00[Europe/Stockholm]").unwrap()
let now = Time.parseInstant("2026-05-01T15:30:00Z").unwrap()
let late = Time.compare(now, deadline.toInstant()) > 0
println(if late { "late" } else { "on time" })lateProperties and relations
Swapping the arguments negates the result:
let a = Time.plainDate(2026, 3, 1)
let b = Time.plainDate(2026, 4, 1)
assert(Time.compare(a, b) == -Time.compare(b, a))For the plain types and Instant, 0 means ==:
let a = Time.plainTime(8, 30)
let b = Time.plainTime(8, 30, 0)
assert(Time.compare(a, b) == 0)
assert(a == b)Two ZonedDateTimes compare as their instants do:
let a = Time.parseZoned("2026-05-04T09:00:00[Asia/Tokyo]").unwrap()
let b = Time.parseZoned("2026-05-04T09:00:00[Europe/London]").unwrap()
assert(Time.compare(a, b) == Time.compare(a.toInstant(), b.toInstant()))The sign of Time.until from a to b is the opposite of the comparison:
let a = Time.plainDate(2026, 3, 1)
let b = Time.plainDate(2026, 4, 1)
assert(Time.until(a, b).sign() == -Time.compare(a, b))
assert(Time.until(b, a).sign() == -Time.compare(b, a))Possible issues
< and the other comparison operators are a compile error on time values:
let a = Time.plainDate(2026, 3, 1)
let b = Time.plainDate(2026, 4, 1)
println(a < b)
// error: < applies to a single numeric kind, string, or a named primitive type over one, not to Time.PlainDateWrite Time.compare(a, b) < 0 instead.
For the same reason, Array.sort, Array.sortBy, Array.min, Array.max, Array.minBy and Array.maxBy do not take time values or time keys:
let dates = [Time.plainDate(2026, 3, 1), Time.plainDate(2025, 12, 24)]
println(dates.sort())
// error: T would be Time.PlainDate, which is not a member of its bound OrderedUse Array.sortWith(Time.compare) to sort; for the earliest or latest, fold with Time.compare as the Array.reduce example in Applications does.
Both arguments must be one type. Comparing an Instant with a ZonedDateTime is a compile error:
let zoned = Time.parseZoned("2026-05-04T09:00:00[Asia/Tokyo]").unwrap()
let instant = Time.fromEpochMillis(1_777_852_800_000)
println(Time.compare(instant, zoned))
// error: cannot use Time.ZonedDateTime where Time.Instant is expectedConvert one of them first. Nine in the morning in Tokyo is midnight UTC:
let zoned = Time.parseZoned("2026-05-04T09:00:00[Asia/Tokyo]").unwrap()
let instant = Time.fromEpochMillis(1_777_852_800_000)
Time.compare(instant, zoned.toInstant()) // 0A PlainTime does not wrap around midnight. A night shift from 22:00 to 02:00 ends at a time that compares as earlier than its start:
Time.compare(Time.plainTime(22), Time.plainTime(2)) // 1Give the times their dates with Time.plainDateTime to order them across midnight:
let start = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(22))
let end = Time.plainDateTime(Time.plainDate(2026, 5, 5), Time.plainTime(2))
Time.compare(start, end) // -1See also
Time.compareDuration · Time.until · Time.since · Time.toInstant · Array.sortWith · Time.Point
