Time.until
func Time.until[T: Time.Point](from: T, to: T, largestUnit: Time.Unit | null = null): Time.DurationTime.until returns the duration from from to to, negative when to is earlier, balanced up to largestUnit. That defaults to Time.Second for an Instant, Time.Hour for a ZonedDateTime or a PlainTime, and Time.Day otherwise; months count from from, one passing when the day of the month reaches that of from, and days between ZonedDateTimes in one zone are the zone's real days. It panics on a unit above Hour for an Instant or a PlainTime or below Day for a PlainDate, and on Day or larger between ZonedDateTimes in different zones.
Details
- The default largest unit is
Secondfor anInstant,Hourfor aZonedDateTimeor aPlainTime, andDayfor aPlainDateor aPlainDateTime. Ask for months or years withlargestUnit:. - Months and years count from
from: a month has passed when the day of the month oftoreaches that offrom. The rest is days from that date, constrained to the month's length, so February 29, 2024 to March 1, 2025 isP1Y1D. Swapping the arguments can change more than the sign. - It panics on a largest unit the type does not have, on
Dayor larger betweenZonedDateTimes in different zones, and when a field of the result does not fit anint.
Basic examples
Time.until(Time.plainDate(2026, 1, 1), Time.plainDate(2026, 3, 15)) // P73D
Time.until(Time.plainDate(2026, 3, 15), Time.plainDate(2026, 1, 1)) // -P73DMonths count from the first date. January 31 to February 28 is no whole month, although January 31 plus a month is February 28:
let jan31 = Time.plainDate(2026, 1, 31)
Time.until(jan31, Time.plainDate(2026, 2, 28), largestUnit: Time.Month{}) // P28D
Time.until(jan31, Time.plainDate(2026, 3, 31), largestUnit: Time.Month{}) // P2MScope
Every point type
An Instant counts in seconds unless you ask for larger units:
let started = Result.unwrap(Time.parseInstant("2026-10-22T14:03:05Z"))
let finished = Result.unwrap(Time.parseInstant("2026-10-22T16:20:35.25Z"))
started.until(finished) // PT8250.25SA PlainTime counts in hours:
Time.plainTime(9).until(Time.plainTime(17, 30)) // PT8H30MA PlainDateTime counts in days and smaller:
let opened = Time.plainDateTime(Time.plainDate(2026, 1, 1), Time.plainTime(8))
let closed = Time.plainDateTime(Time.plainDate(2026, 3, 15), Time.plainTime(20, 15))
opened.until(closed) // P73DT12H15MA ZonedDateTime counts in hours. New York sets its clocks forward on March 8, 2026, so noon to noon across it is 23 hours, or one of the zone's real days:
let ny = Result.unwrap(Time.zone("America/New_York"))
let saturday = Time.plainDateTime(Time.plainDate(2026, 3, 7), Time.plainTime(12)).toZoned(ny)
let sunday = Time.plainDateTime(Time.plainDate(2026, 3, 8), Time.plainTime(12)).toZoned(ny)
saturday.until(sunday) // PT23H
saturday.until(sunday, largestUnit: Time.Day{}) // P1DBetween two zones, hours and smaller still work:
let tokyo = Result.unwrap(Time.zone("Asia/Tokyo"))
let t = Time.fromEpochMillis(0)
t.inZone(Time.utc).until(t.add(30h).inZone(tokyo)) // PT30HThe same point, and pipes
A point until itself is a zero duration:
let day = Time.plainDate(2026, 5, 4)
day.until(day) // PT0SIn a pipe, the piped value is from:
Time.plainDate(2026, 1, 1) | Time.until(Time.plainDate(2026, 12, 25)) // P358DNamed arguments
largestUnit: sets the largest field of the result; everything above it is carried down:
let opened = Time.plainDateTime(Time.plainDate(2026, 1, 1), Time.plainTime(8))
let closed = Time.plainDateTime(Time.plainDate(2026, 3, 15), Time.plainTime(20, 15))
opened.until(closed, largestUnit: Time.Year{}) // P2M14DT12H15M
opened.until(closed, largestUnit: Time.Week{}) // P10W3DT12H15M
opened.until(closed, largestUnit: Time.Minute{}) // PT105855MOn an Instant, Time.Hour{} is the largest unit there is:
let started = Result.unwrap(Time.parseInstant("2026-10-22T14:03:05Z"))
let finished = Result.unwrap(Time.parseInstant("2026-10-22T16:20:35.25Z"))
started.until(finished, largestUnit: Time.Hour{}) // PT2H17M30.25SFebruary 28, 2025 does not reach day 29, so no year has passed. On March 1 a year has passed, and the one day left counts from February 28, the constrained date:
let leap = Time.plainDate(2024, 2, 29)
leap.until(Time.plainDate(2025, 2, 28), largestUnit: Time.Year{}) // P11M30D
leap.until(Time.plainDate(2025, 3, 1), largestUnit: Time.Year{}) // P1Y1DAll three arguments can be named:
let start = Time.plainDate(2026, 1, 1)
let end = Time.plainDate(2026, 12, 25)
Time.until(from: start, to: end, largestUnit: Time.Month{}) // P11M24DApplications
Days left until a deadline:
func daysLeft(today: Time.PlainDate, deadline: Time.PlainDate): int {
today.until(deadline).part(Time.Day{})
}
daysLeft(Time.plainDate(2026, 11, 11), Time.plainDate(2026, 12, 31)) // 50
daysLeft(Time.plainDate(2027, 1, 2), Time.plainDate(2026, 12, 31)) // -2How many whole months a subscription has run, for billing:
func monthsBilled(start: Time.PlainDate, today: Time.PlainDate): int {
start.until(today, largestUnit: Time.Month{}).part(Time.Month{})
}
let start = Time.plainDate(2026, 1, 15)
monthsBilled(start, Time.plainDate(2026, 4, 14)) // 2
monthsBilled(start, Time.plainDate(2026, 4, 15)) // 3Hours worked per shift, from clock-in and clock-out times:
type Shift = { start: Time.PlainTime, end: Time.PlainTime }
let shifts = [
Shift{ start: Time.plainTime(9), end: Time.plainTime(17, 30) },
Shift{ start: Time.plainTime(8, 15), end: Time.plainTime(12) },
]
for shift in shifts {
println(shift.start.until(shift.end).total(Time.Hour{}))
}8.5
3.75Properties and relations
For exact units, adding the result to from gives to, and swapping the arguments negates it:
let a = Result.unwrap(Time.parseInstant("2026-10-22T14:03:05Z"))
let b = Result.unwrap(Time.parseInstant("2026-10-23T09:00:00Z"))
assert(a.add(a.until(b)) == b)
assert(b.until(a) == a.until(b).negate())
assert(a.until(b) == b.since(a))With calendar units the anchor matters, so swapping the arguments can give a different length:
let feb29 = Time.plainDate(2024, 2, 29)
let mar31 = Time.plainDate(2024, 3, 31)
feb29.until(mar31, largestUnit: Time.Month{}) // P1M2D
mar31.until(feb29, largestUnit: Time.Month{}) // -P1MPossible issues
A PlainTime does not wrap around midnight. From 23:00 to 01:00 is 22 hours back, not 2 hours forward:
Time.plainTime(23).until(Time.plainTime(1)) // -PT22HA largest unit the type does not have panics:
let a = Time.fromEpochMillis(0)
let b = Time.fromEpochMillis(90_000_000)
a.until(b, largestUnit: Time.Day{}) // panic: Time.until: an instant has no days
Time.plainDate(2026, 1, 1).until(Time.plainDate(2026, 1, 2), largestUnit: Time.Hour{})
// panic: Time.until: a date has no hours
Time.plainTime(9).until(Time.plainTime(10), largestUnit: Time.Day{})
// panic: Time.until: a time has no daysDays between two zones do not line up, so Day or larger panics:
let tokyo = Result.unwrap(Time.zone("Asia/Tokyo"))
let t = Time.fromEpochMillis(0)
t.inZone(Time.utc).until(t.inZone(tokyo), largestUnit: Time.Day{})
// panic: Time.until: UTC and Asia/Tokyo are different zones; a difference in days or larger needs one zoneA long span in a small unit can overflow an int. Three centuries in nanoseconds panics:
let a = Time.fromEpochMillis(0)
let b = Result.unwrap(Time.parseInstant("2300-01-01T00:00:00Z"))
a.until(b, largestUnit: Time.Nanosecond{})
// panic: Time.until: a balanced field does not fit an intBoth points must have the same type:
let start = Time.fromEpochMillis(0)
let end = Time.fromEpochMillis(1000)
let d = start.until(end.inZone(Time.utc)) // error: cannot use Time.ZonedDateTime where Time.Instant is expectedSee also
Time.since, Time.add, Time.negate, Time.total, Time.part, Time.roundDuration, Time.compare, Time.Unit
