Sub topic 2
Exact Moments with Temporal.Instant
This chapter assumes you know modern JavaScript, exceptions, and the legacy Date object. It also assumes Temporal is available, either natively or through @js-temporal/polyfill.
Temporal.Instant
A Temporal.Instant is one exact point on the global timeline. It counts nanoseconds since the Unix epoch and has no time zone or calendar. Use it for timestamps such as log entries or the moment an event happened.
Time Zones and IANA Identifiers
Temporal names time zones with IANA identifiers such as Asia/Tokyo or America/New_York. Prefer these over fixed offsets like +09:00. An identifier carries the zone's daylight saving rules. A fixed offset does not.
ZonedDateTime
A ZonedDateTime combines an exact instant with a time zone. That makes it both an exact moment and a wall-clock reading.
const instant = Temporal.Instant.from('2024-03-15T12:00:00Z');
const tokyo = instant.toZonedDateTimeISO('Asia/Tokyo');
console.log(tokyo.toString()); // '2024-03-15T21:00:00+09:00[Asia/Tokyo]'
const paris = tokyo.withTimeZone('Europe/Paris');
console.log(paris.toPlainTime().toString()); // '13:00:00'Calendar units and exact units behave differently across a daylight saving change:
const noon = Temporal.ZonedDateTime.from('2024-03-09T12:00[America/New_York]');
console.log(noon.add({ days: 1 }).toString()); // '2024-03-10T12:00:00-04:00[America/New_York]'
console.log(noon.add({ hours: 24 }).toString()); // '2024-03-10T13:00:00-04:00[America/New_York]'Arithmetic and Overflow
add and subtract move a value by a duration. with replaces individual fields. Each method returns a new object. When a result lands on a date that doesn't exist, the overflow option decides what happens. The default, 'constrain', clamps the result to the nearest valid date. 'reject' throws a RangeError instead.
const jan31 = Temporal.PlainDate.from('2024-01-31');
console.log(jan31.add({ months: 1 }).toString()); // '2024-02-29'
console.log(jan31.with({ day: 1 }).toString()); // '2024-01-01'
try {
jan31.add({ months: 1 }, { overflow: 'reject' });
} catch (e) {
console.log(e instanceof RangeError); // true
}Durations, until, and since
A Temporal.Duration is a length of time, such as "1 hour 30 minutes". a.until(b) returns the duration from a to b. a.since(b) returns the duration from b to a. The largestUnit option sets the biggest unit the result can use.
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(end.since(start).toString()); // 'P74D'
const meeting = Temporal.Duration.from({ hours: 1, minutes: 30 });
console.log(meeting.total({ unit: 'minute' })); // 90| Need | Use |
|---|---|
| Exact timestamp | `Instant` |
| Local time in a place | `ZonedDateTime` |
| Length of time | `Duration` |
Exercise
A user schedules a reminder for 09:00 every day in America/New_York. Should the next reminder be computed with add({ days: 1 }) or add({ hours: 24 }), and why?
Show answer
Use add({ days: 1 }). It keeps the wall-clock time at 09:00 across daylight saving changes. Adding 24 hours adds exact elapsed time, so the reminder would shift to 08:00 or 10:00 after a transition.
Exercise
What does Temporal.PlainDate.from('2023-03-31').subtract({ months: 1 }).toString() return, and what changes if you pass { overflow: 'reject' }?
Show answer
It returns '2023-02-28', because February 31 doesn't exist and the default 'constrain' clamps to the last valid day. With 'reject', the call throws a RangeError.