Time.add
func Time.add[T: Time.Point](t: T, d: Time.Duration): TTime.add returns t moved by d. The calendar part goes first, years then months with the day constrained, then weeks and days, and hours and smaller after; on a ZonedDateTime the calendar result is placed in the zone by Time.Compatible before the exact part moves the instant. A PlainDate takes the exact part as whole 24-hour days and a PlainTime ignores the calendar part and wraps around the clock. It panics if t is an Instant and d has years, months, weeks or days.
Details
- Months and years constrain the day to the end of a shorter month instead of panicking.
- One duration applies its years and months before its days, which can differ from adding the days first.
Basic examples
Time.add(Time.plainDate(2024, 1, 31), Time.duration(months: 1)) // 2024-02-29
Time.add(Time.plainTime(23, 30), 2h) // 01:30:00A negative duration moves back:
Time.add(Time.plainDate(2026, 3, 1), Time.duration(days: -1)) // 2026-02-28Scope
Every point type
A PlainDate takes calendar units and whole days of the exact part:
let day = Time.plainDate(2026, 1, 1)
day.add(Time.duration(years: 1, weeks: 2)) // 2027-01-15
day.add(47h) // 2026-01-02
day.add(Time.duration(hours: -1)) // 2026-01-01A PlainTime wraps around the clock and ignores days:
let noon = Time.plainTime(12)
noon.add(13h) // 01:00:00
noon.add(Time.duration(days: 3, hours: 1)) // 13:00:00
noon.add(Time.duration(milliseconds: -500)) // 11:59:59.5A PlainDateTime carries the clock into the date:
let eve = Time.plainDateTime(Time.plainDate(2026, 12, 31), Time.plainTime(23))
eve.add(2h) // 2027-01-01T01:00:00
eve.add(Time.duration(days: 1, hours: 2)) // 2027-01-02T01:00:00An Instant takes hours and smaller units:
let start = Time.fromEpochMillis(0)
start.add(36h30m) // 1970-01-02T12:30:00Z
start.add(Time.negate(1s)) // 1969-12-31T23:59:59ZA ZonedDateTime keeps its zone:
let utc = Time.fromEpochMillis(0).inZone(Time.utc)
utc.add(Time.duration(months: 1, hours: 6)) // 1970-02-01T06:00:00+00:00[UTC]Durations with several fields
Months go first, then days, then the exact part. From January 30, a month gives February 29 and the day after it is March 1:
let day = Time.plainDate(2024, 1, 30)
day.add(Time.duration(months: 1, days: 1)) // 2024-03-01
day.add(Time.duration(days: 1)).add(Time.duration(months: 1)) // 2024-02-29Zero and pipes
Adding a zero duration gives the same value:
let day = Time.plainDate(2026, 5, 4)
day.add(Time.duration()) == day // trueCall it through . or in a pipe:
Time.plainDate(2026, 5, 4).add(Time.duration(days: 7)) // 2026-05-11
Time.plainDate(2026, 5, 4) | Time.add(Time.duration(days: 7)) // 2026-05-11Days and hours across a daylight-saving change
On a ZonedDateTime, a day moves the wall clock and an hour moves the instant. New York sets its clocks forward on March 8, 2026, so that day has 23 hours:
let ny = Result.unwrap(Time.zone("America/New_York"))
let saturday = Time.plainDateTime(Time.plainDate(2026, 3, 7), Time.plainTime(12)).toZoned(ny)
saturday.add(Time.duration(days: 1)) // 2026-03-08T12:00:00-04:00[America/New_York]
saturday.add(24h) // 2026-03-08T13:00:00-04:00[America/New_York]It sets them back on November 1, 2026, so that day has 25 hours and 24 hours end an hour short of noon:
let ny = Result.unwrap(Time.zone("America/New_York"))
let saturday = Time.plainDateTime(Time.plainDate(2026, 10, 31), Time.plainTime(12)).toZoned(ny)
saturday.add(Time.duration(days: 1)) // 2026-11-01T12:00:00-05:00[America/New_York]
saturday.add(24h) // 2026-11-01T11:00:00-05:00[America/New_York]A day that lands in the skipped hour moves forward out of it:
let ny = Result.unwrap(Time.zone("America/New_York"))
let early = Time.plainDateTime(Time.plainDate(2026, 3, 7), Time.plainTime(2, 30)).toZoned(ny)
early.add(Time.duration(days: 1)) // 2026-03-08T03:30:00-04:00[America/New_York]Applications
A due date 30 days after an invoice:
func dueDate(issued: Time.PlainDate): Time.PlainDate {
issued.add(Time.duration(days: 30))
}
dueDate(Time.plainDate(2026, 11, 15)) // 2026-12-15A deadline five minutes from a fixed instant, checked with Time.compare:
let started = Result.unwrap(Time.parseInstant("2026-10-22T14:03:05Z"))
let deadline = started.add(5m)
let now = Result.unwrap(Time.parseInstant("2026-10-22T14:09:00Z"))
Time.compare(now, deadline) > 0 // trueMonthly billing dates anchored on the 31st. Adding i months to the first date keeps the day where the month allows it:
let first = Time.plainDate(2026, 1, 31)
for i in Iter.range(0, 4) {
println(first.add(Time.duration(months: i)))
}2026-01-31
2026-02-28
2026-03-31
2026-04-30A weekly meeting stays at 09:00 in its zone across the change of clocks:
let ny = Result.unwrap(Time.zone("America/New_York"))
let first = Time.plainDateTime(Time.plainDate(2026, 2, 26), Time.plainTime(9)).toZoned(ny)
for week in Iter.range(0, 3) {
println(first.add(Time.duration(weeks: week)))
}2026-02-26T09:00:00-05:00[America/New_York]
2026-03-05T09:00:00-05:00[America/New_York]
2026-03-12T09:00:00-04:00[America/New_York]Properties and relations
Time.subtract adds the negated duration:
let t = Time.plainDateTime(Time.plainDate(2026, 6, 1), Time.plainTime(8, 15))
let d = Time.duration(days: 2, hours: 3)
assert(t.subtract(d) == t.add(d.negate()))For exact units on an Instant, subtracting undoes adding, and Time.until measures the step:
let t = Time.fromEpochMillis(1_000_000)
let d = 5h7s
assert(t.add(d).subtract(d) == t)
assert(t.until(t.add(d)) == 18007s)Possible issues
An Instant has no calendar, so a day panics. Use 24 hours, or convert to a ZonedDateTime:
let start = Time.fromEpochMillis(0)
start.add(Time.duration(days: 1)) // panic: Time.add: an instant has no calendar, so it takes no days; convert it with Time.inZone first
start.add(24h) // 1970-01-02T00:00:00ZMonths do not undo each other. The day is constrained at each step, so two single months from January 31 end on March 28, while two months at once end on March 31:
let day = Time.plainDate(2025, 1, 31)
day.add(Time.duration(months: 1)).add(Time.duration(months: 1)) // 2025-03-28
day.add(Time.duration(months: 2)) // 2025-03-31
day.add(Time.duration(months: 1)).subtract(Time.duration(months: 1)) // 2025-01-28A PlainDate drops what is less than a day, so small steps add up to nothing:
let day = Time.plainDate(2026, 1, 1)
day.add(12h).add(12h) // 2026-01-01
day.add(24h) // 2026-01-02A value typed with a union such as Time.Dated does not pass until an is test narrows it:
func tomorrow(t: Time.Dated): Time.Dated {
Time.add(t, Time.duration(days: 1)) // error: T would be Time.Dated, which is not a single member of its bound Time.Point; narrow first
}Each branch of an is test narrows t to a single member, and there the call passes:
func tomorrow(t: Time.Dated): Time.Dated {
let day = Time.duration(days: 1)
if t is Time.ZonedDateTime {
t.add(day)
} else if t is Time.PlainDate {
t.add(day)
} else {
t.add(day)
}
}
println(tomorrow(Time.plainDate(2026, 5, 4)))2026-05-05A result beyond the range of its type panics:
Time.plainDate(275760, 9, 13).add(Time.duration(days: 2)) // panic: Time.add: outside the range of time valuesSee also
Time.subtract, Time.duration, Time.until, Time.since, Time.with, Time.addDuration, Time.compare
