Sub topic 14
Measuring the Time Between Two Values with until and since
This chapter assumes you have read the earlier chapters on Plain types, Instant, ZonedDateTime, and Temporal.Duration. It also assumes you can run ES module code in Node.js or a browser. Every example expects Temporal to be in scope, either built into the runtime or loaded from @js-temporal/polyfill as shown in the chapter on runtime support.
The working model
Every Temporal type that holds a point in time has two methods for measuring a gap: until and since. Both return a Temporal.Duration.
a.until(b)measures fromaforward tob. The result is positive whenbis later.a.since(b)measures frombforward toa. The result is positive whenais later.
That means start.until(end) and end.since(start) give the same duration. Pick whichever one reads more naturally where you use it.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const start = Temporal.PlainDate.from('2024-01-01');
const end = Temporal.PlainDate.from('2024-03-15');
console.log(start.until(end).toString()); // 'P74D'
console.log(end.since(start).toString()); // 'P74D'
console.log(start.since(end).toString()); // '-P74D'
console.log(start.until('2024-03-15').toString()); // 'P74D' (strings are converted)The argument doesn't have to be an object of the same type. It can be anything that the type's from() accepts, such as an ISO string or a property bag. It cannot be a different Temporal type, though, and both values must use the same calendar. A mismatch throws a RangeError.
Controlling the units of the result
By default you get one large unit and nothing bigger. For PlainDate, that unit is days, so you got P74D above rather than "2 months and 14 days". You can change this with options:
| Option | Meaning | Default |
|---|---|---|
| `largestUnit` | Biggest unit allowed in the result | Depends on the type (see below) |
| `smallestUnit` | Smallest unit kept; anything smaller is rounded away | `'nanosecond'` |
| `roundingIncrement` | Round to multiples of this many `smallestUnit` | `1` |
| `roundingMode` | How to round, for example `'trunc'`, `'halfExpand'`, `'ceil'` | `'trunc'` |
Each type has its own default largestUnit:
| Type | Default `largestUnit` | Allowed units |
|---|---|---|
| `PlainDate`, `PlainDateTime` | `'day'` | All units |
| `PlainYearMonth` | `'year'` | Years and months |
| `PlainTime` | `'hour'` | Hours and smaller |
| `Instant` | `'second'` | Hours and smaller |
| `ZonedDateTime` | `'hour'` | All units |
const start = Temporal.PlainDate.from('2024-01-01');
const end = Temporal.PlainDate.from('2024-03-15');
console.log(start.until(end, { largestUnit: 'month' }).toString()); // 'P2M14D'
console.log(start.until(end, { largestUnit: 'year' }).toString()); // 'P2M14D'
// 74 days = 10 weeks and 4 days
console.log(start.until(end, { smallestUnit: 'week' }).toString()); // 'P10W'
console.log(
start.until(end, { smallestUnit: 'week', roundingMode: 'halfExpand' }).toString()
); // 'P11W'When you set only smallestUnit and it is larger than the default largestUnit, Temporal raises largestUnit to match. That is why smallestUnit: 'week' works above without a largestUnit. The full rounding rules are covered in the chapter on rounding.
Exact differences with Instant
Instant has no calendar and no time zone, so it can only report time units. The default unit is seconds. Asking for 'day' or larger throws a RangeError.
const a = Temporal.Instant.from('2024-03-15T12:00:00Z');
const b = Temporal.Instant.from('2024-03-15T13:30:15Z');
console.log(a.until(b).toString()); // 'PT5415S'
console.log(a.until(b, { largestUnit: 'hour' }).toString()); // 'PT1H30M15S'ZonedDateTime: elapsed hours vs calendar days
For ZonedDateTime, the choice of largestUnit changes what the answer means. Hours measure real elapsed time. Days measure calendar days in that time zone, and a calendar day is not always 24 hours long.
const before = Temporal.ZonedDateTime.from('2024-03-09T12:00[America/New_York]');
const after = Temporal.ZonedDateTime.from('2024-03-10T12:00[America/New_York]');
console.log(before.until(after).toString()); // 'PT23H'
console.log(before.until(after, { largestUnit: 'day' }).toString()); // 'P1D'The clocks sprang forward overnight, so noon to noon took 23 real hours but one calendar day. Use the default when you are billing for time or measuring a timeout. Use 'day' or larger when you care about dates on a wall calendar, such as "3 days until the event".
Calendar units depend on a time zone. If the two values are in different zones and largestUnit is 'day' or larger, Temporal throws a RangeError. Convert one value first with withTimeZone(). With hours or smaller, mixed zones are fine, because elapsed time doesn't depend on a zone.
Common errors
- Reversed sign.
a.since(b)isa − b. If you see negative durations you didn't expect, you probably swapped the method or the arguments. - PlainTime does not wrap past midnight.
Temporal.PlainTime.from('23:00').until('01:00')is-PT22H, notPT2H. APlainTimehas no date, so it cannot know that 01:00 means the next day. UsePlainDateTimefor overnight spans. - Summing calendar durations.
P2M14Dhas no fixed length. To get one number, calltotalwith arelativeToanchor, for exampleduration.total({ unit: 'day', relativeTo: start }), which returns74. - Relying on the default unit for display. A raw
P74Dis rarely what users want to read. PicklargestUniton purpose.
Round-tripping
For Plain types, adding the result back gives you the end value: start.add(start.until(end)) equals end. This is useful in tests. Rounding with smallestUnit breaks the property on purpose, because the rounded duration no longer describes the exact gap.
Exercise
A subscription started at 2024-01-01 and you check it on 2024-03-15. Write one expression that reports the elapsed time as whole months only, rounded to the nearest month. What does it print?
Show answer
Temporal.PlainDate.from('2024-01-01').until('2024-03-15', { largestUnit: 'month', smallestUnit: 'month', roundingMode: 'halfExpand' }).toString() prints 'P2M'. The extra 14 days are less than half of March, so the result rounds down.
Exercise
You compute a.until(b, { largestUnit: 'day' }) where a is in Europe/Paris and b is in Asia/Tokyo, and it throws. Why, and what are two ways to fix it?
Show answer
Calendar units such as days depend on a time zone, so Temporal requires both ZonedDateTime values to share one when largestUnit is 'day' or larger, and it throws a RangeError when they don't. One fix is to convert first, for example a.until(b.withTimeZone(a.timeZoneId), { largestUnit: 'day' }). The other is to measure elapsed time with largestUnit 'hour' or smaller, which allows mixed time zones.
Exercise
Temporal.PlainTime.from('22:30').until('00:15') returns '-PT22H15M'. How do you get the overnight gap of 1 hour 45 minutes?
Show answer
Give the times dates. For example, Temporal.PlainDateTime.from('2024-03-15T22:30').until('2024-03-16T00:15').toString() returns 'PT1H45M'.