Sub topic 12
Measuring the Time Between Two Values with until and since
This chapter assumes you know how to create Temporal values with from() and how Temporal.Duration stores and prints amounts of time. The examples import the polyfill. If your runtime has native Temporal, you can use the global instead.
The working model
Every Temporal type that holds a date or time has two methods for measuring a gap between two values:
a.until(b)measures fromaforward tob.a.since(b)measures frombforward toa.
Both methods return a Temporal.Duration. When the second value comes before the first, until returns a negative duration. since reverses that, so a.since(b) gives the same result as b.until(a).
Controlling the units with largestUnit
The result is expressed in a default largest unit, which depends on the type. You can change it with the largestUnit option.
import { Temporal } from '@js-temporal/polyfill';
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(start.until(end, { largestUnit: 'month' }).toString()); // 'P2M14D'
console.log(start.until(end, { largestUnit: 'week' }).toString()); // 'P10W4D'
console.log(end.since(start).toString()); // 'P74D'
console.log(start.since(end).toString()); // '-P74D'The defaults are:
| Type | Default largestUnit |
|---|---|
| PlainDate, PlainDateTime | day |
| PlainYearMonth | year |
| PlainTime | hour |
| ZonedDateTime | hour |
| Instant | second |
The Instant default often catches people out:
import { Temporal } from '@js-temporal/polyfill';
const a = Temporal.Instant.from('2024-03-15T12:00:00Z');
const b = Temporal.Instant.from('2024-03-15T13:30:00Z');
console.log(a.until(b).toString()); // 'PT5400S'
console.log(a.until(b, { largestUnit: 'hour' }).toString()); // 'PT1H30M'Trimming the result with smallestUnit
smallestUnit drops the finer units. By default the leftover is truncated (roundingMode: 'trunc'). The rounding chapter covers the other modes.
import { Temporal } from '@js-temporal/polyfill';
const start = Temporal.PlainDate.from('2024-01-01');
const end = Temporal.PlainDate.from('2024-03-15');
const months = start.until(end, { largestUnit: 'month', smallestUnit: 'month' });
console.log(months.toString()); // 'P2M'Common errors
- Asking an Instant for months or years. An
Instanthas no calendar, solargestUnit: 'month'throws aRangeError. Convert it to aZonedDateTimefirst. - Setting smallestUnit larger than largestUnit. For example,
{ largestUnit: 'day', smallestUnit: 'month' }throws aRangeError. - Using different time zones. Calling
untilon twoZonedDateTimevalues in different zones, withlargestUnitof'day'or larger, throws aRangeError. UsewithTimeZoneto put both values in the same zone. - Expecting a single number. The result is a
Duration. Calltotal({ unit: 'day' })on it to get a plain number.
Exercise
A subscription starts on 2024-01-31 and ends on 2024-04-30. Write a call that reports the gap in whole months only, and give the output string.
Show answer
Temporal.PlainDate.from('2024-01-31').until('2024-04-30', { largestUnit: 'month', smallestUnit: 'month' }).toString() returns 'P2M'. Counting forward from January 31 gives 2 months and 30 days (P2M30D), because a third month would land on May 1 after clamping. The default truncation then drops the 30 days.