Time.since
func Time.since[T: Time.Point](t: T, other: T, largestUnit: Time.Unit | null = null): Time.DurationTime.since returns the duration from other to t, counted from t. It is Time.negate(Time.until(t, other)), and for calendar units can differ from Time.until(other, t), whose anchor is other. It panics as Time.until does.
Details
- The result is positive when
tis later thanother, the opposite sign ofTime.until(t, other). - The default largest unit is
Secondfor anInstant,Hourfor aZonedDateTimeor aPlainTime, andDayfor the others. - It panics on a largest unit the type does not have, and on
Dayor larger betweenZonedDateTimes in different zones.
Basic examples
let feb29 = Time.plainDate(2024, 2, 29)
let mar31 = Time.plainDate(2024, 3, 31)
Time.since(mar31, feb29) // P31D
Time.since(feb29, mar31) // -P31DTime.since counts months back from its first argument, so it can differ from Time.until:
let feb29 = Time.plainDate(2024, 2, 29)
let mar31 = Time.plainDate(2024, 3, 31)
Time.since(mar31, feb29, largestUnit: Time.Month{}) // P1M
Time.until(feb29, mar31, largestUnit: Time.Month{}) // P1M2DScope
Every point type
An Instant counts in seconds unless largestUnit: names a larger unit:
let started = Result.unwrap(Time.parseInstant("2026-10-22T14:03:05Z"))
let finished = Result.unwrap(Time.parseInstant("2026-10-22T16:20:35.25Z"))
finished.since(started) // PT8250.25SA PlainTime counts in hours:
Time.plainTime(17, 30).since(Time.plainTime(9, 0)) // PT8H30M
Time.plainTime(9, 0).since(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))
closed.since(opened) // 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)
sunday.since(saturday) // PT23H
sunday.since(saturday, largestUnit: Time.Day{}) // P1DThe same point, and pipes
A point since itself is a zero duration:
let day = Time.plainDate(2026, 5, 4)
day.since(day) // PT0SIn a pipe, the piped value is t, the later point of a positive result:
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))
closed | Time.since(opened) // P73DT12H15MNamed 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))
closed.since(opened, largestUnit: Time.Year{}) // P2M14DT12H15M
closed.since(opened, largestUnit: Time.Week{}) // P10W3DT12H15M
closed.since(opened, largestUnit: Time.Hour{}) // PT1764H15MOn 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"))
finished.since(started, largestUnit: Time.Hour{}) // PT2H17M30.25SApplications
A person's age in whole years, read from the year field of a duration counted in years:
func age(birthday: Time.PlainDate, today: Time.PlainDate): int {
today.since(birthday, largestUnit: Time.Year{}).part(Time.Year{})
}
age(Time.plainDate(1990, 7, 15), Time.plainDate(2026, 7, 14)) // 35
age(Time.plainDate(1990, 7, 15), Time.plainDate(2026, 7, 15)) // 36How long a job took, in minutes:
let started = Result.unwrap(Time.parseInstant("2026-10-22T14:03:05Z"))
let finished = Result.unwrap(Time.parseInstant("2026-10-22T14:10:35Z"))
finished.since(started).total(Time.Minute{}) // 7.5A short label for how long ago something happened:
func ago(then: Time.Instant, now: Time.Instant): string {
let d = now.since(then, largestUnit: Time.Hour{})
let hours = d.part(Time.Hour{})
let minutes = d.part(Time.Minute{})
if hours > 0 { "${hours} h ago" } else if minutes > 0 { "${minutes} min ago" } else { "just now" }
}
let now = Result.unwrap(Time.parseInstant("2026-10-22T14:00:00Z"))
for stamp in ["2026-10-22T13:59:30Z", "2026-10-22T13:45:00Z", "2026-10-22T09:10:00Z"] {
println(ago(Result.unwrap(Time.parseInstant(stamp)), now))
}just now
15 min ago
4 h agoProperties and relations
Time.since negates Time.until with the same arguments, and for exact units it equals Time.until with the arguments swapped:
let a = Result.unwrap(Time.parseInstant("2026-10-22T14:03:05Z"))
let b = Result.unwrap(Time.parseInstant("2026-10-23T09:00:00Z"))
assert(b.since(a) == Time.negate(Time.until(b, a)))
assert(b.since(a) == Time.until(a, b))
assert(a.add(b.since(a)) == b)Possible issues
The arguments read as "t since other", the reverse order of Time.until. Swapping them flips the sign:
let earlier = Time.plainDate(2026, 1, 1)
let later = Time.plainDate(2026, 1, 8)
later.since(earlier) // P7D
earlier.since(later) // -P7DA PlainTime does not wrap around midnight. From 23:00 to 01:00 is 22 hours back, not 2 hours forward:
Time.plainTime(1).since(Time.plainTime(23)) // -PT22HA largest unit the type does not have panics:
let a = Time.fromEpochMillis(0)
let b = Time.fromEpochMillis(90_000_000)
b.since(a, largestUnit: Time.Day{}) // panic: Time.since: an instant has no days
Time.plainDate(2026, 1, 2).since(Time.plainDate(2026, 1, 1), largestUnit: Time.Hour{})
// panic: Time.since: a date has no hoursDays 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).since(t.inZone(tokyo), largestUnit: Time.Day{})
// panic: Time.since: UTC and Asia/Tokyo are different zones; a difference in days or larger needs one zoneBoth points must have the same type:
let start = Time.fromEpochMillis(0)
let end = Time.fromEpochMillis(1000)
let d = end.since(start.inZone(Time.utc)) // error: cannot use Time.ZonedDateTime where Time.Instant is expectedSee also
Time.until, Time.negate, Time.add, Time.subtract, Time.total, Time.part, Time.roundDuration, Time.Unit
