Sub topic 2
A Map of the Temporal Types: Exact Time vs Wall-Clock Time
This chapter assumes you know modern JavaScript syntax, can run ES modules in Node.js or a browser, and have used the legacy Date object. It also assumes Temporal is available, either natively or through the polyfill from the previous chapter.
Date uses one type for every job. Temporal splits that job across several types. Each type holds only the information it needs. Picking a type means answering one question first: are you describing an exact moment, or what a clock and calendar show?
Two kinds of time
Exact time is one point on the global timeline. It is the same everywhere, for example "the moment the server received this request." Temporal.Instant stores it as nanoseconds since the Unix epoch, with no time zone attached.
Wall-clock time is what a person reads off a calendar or a clock, for example "March 15 at 9:00." On its own it does not pin down a moment. 9:00 in Tokyo and 9:00 in Paris are different instants. Temporal calls these the Plain types, because they have no time zone.
Temporal.ZonedDateTime connects the two. It combines an exact instant with an IANA time zone and a calendar, so it knows both the moment and the local clock reading.
The types at a glance
| Type | Holds | Typical use |
|---|---|---|
| `Instant` | Exact moment, no zone | Log timestamps, event ordering |
| `ZonedDateTime` | Instant + time zone + calendar | Meetings, flights, anything that crosses DST |
| `PlainDateTime` | Date and time, no zone | A local appointment before the zone is known |
| `PlainDate` | Year, month, day | Birthdays, due dates |
| `PlainTime` | Hour down to nanosecond | Store opening hours |
| `PlainYearMonth` | Year and month | Billing periods, credit card expiry |
| `PlainMonthDay` | Month and day | Yearly holidays |
| `Duration` | A length of time | "2 hours 30 minutes" |
Duration belongs to neither group. It measures an amount of time, not a position in time.
Each type prints its own ISO 8601 form, and the format shows exactly what the type holds:
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
console.log(Temporal.Instant.from('2024-03-15T12:00:00Z').toString());
// '2024-03-15T12:00:00Z'
console.log(Temporal.ZonedDateTime.from('2024-03-15T08:00[America/New_York]').toString());
// '2024-03-15T08:00:00-04:00[America/New_York]'
console.log(Temporal.PlainDateTime.from('2024-03-15T08:00').toString()); // '2024-03-15T08:00:00'
console.log(Temporal.PlainDate.from('2024-03-15').toString()); // '2024-03-15'
console.log(Temporal.PlainTime.from('08:00').toString()); // '08:00:00'
console.log(Temporal.PlainYearMonth.from('2024-03').toString()); // '2024-03'
console.log(Temporal.PlainMonthDay.from('03-15').toString()); // '03-15'
console.log(Temporal.Duration.from({ hours: 2, minutes: 30 }).toString()); // 'PT2H30M'Common mistakes
A related mistake goes the other way: storing a birthday as an Instant or ZonedDateTime. When the value is shown in another zone, it can land on the previous or next day. A birthday is a calendar date, so use PlainDate.
When you are unsure, pick the narrowest type that holds everything you need. Narrow types prevent whole categories of bugs.
Exercise
Pick a Temporal type for each value: (a) the time a payment was processed, for an audit log; (b) a recurring "office opens at 09:00" setting; (c) a video call on 2024-06-01 at 15:00 in Berlin; (d) Christmas Day each year.
Show answer
(a) Temporal.Instant, because only the exact moment matters. (b) Temporal.PlainTime, because it is a clock reading with no date or zone. (c) Temporal.ZonedDateTime, because the call happens at a specific moment tied to the Europe/Berlin time zone. (d) Temporal.PlainMonthDay, because it repeats every year with no year or time.
Exercise
Why does Temporal.PlainDateTime.from('2024-03-15T08:00') not identify a single moment on the global timeline?
Show answer
It has no time zone or offset. 08:00 on that date happens at a different exact instant in each time zone, so the value is only a wall-clock reading until you attach a zone, for example by converting it to a ZonedDateTime.