Sub topic 3
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 set up in the previous chapter.
Temporal replaces Date with a family of types instead of a single object. Each type covers one kind of date or time. Most mistakes with Temporal come from picking the wrong type, so this chapter builds a map of the types before later chapters look at each one in detail.
Two Kinds of Time
Temporal is built on one distinction.
- Exact time is a single point on the global timeline, such as the moment a payment was recorded. It is the same everywhere on Earth. Internally it is a count of nanoseconds since the Unix epoch (1970-01-01T00:00Z).
- Wall-clock time is what a calendar and a clock on a wall show: "March 15, 9:00". It means nothing about the global timeline until you say where the wall is.
A time zone connects the two. It is a set of rules that maps exact times to wall-clock times for a region, including its daylight saving changes. Date mixes both ideas into one object. That is why getHours() silently depends on the machine's local zone. Temporal keeps them apart and makes you convert between them explicitly.
The Types at a Glance
| Type | Represents | Example string | Typical use |
|---|---|---|---|
| `Instant` | An exact moment, no zone or calendar | `2024-03-15T12:00:00Z` | Log timestamps, event ordering |
| `ZonedDateTime` | An exact moment plus a time zone and calendar | `2024-03-15T08:00:00-04:00[America/New_York]` | Meetings, flights, anything tied to a place |
| `PlainDateTime` | A wall-clock date and time with no zone | `2024-03-15T09:00:00` | A form input before the zone is known |
| `PlainDate` | A calendar date | `2024-03-15` | Birthdays, due dates, holidays |
| `PlainTime` | A clock time | `09:00:00` | "Store opens at 9" |
| `PlainYearMonth` | A year and month | `2024-03` | Billing periods, credit card expiry |
| `PlainMonthDay` | A month and day with no year | `03-15` | Recurring anniversaries |
| `Duration` | A length of time | `PT1H30M` | Timeouts, "3 days from now" |
Temporal.Now is not a type. It is a namespace of functions that return the current time as any of the types above.
Moving Along the Map
Going down the map drops information and never fails. Going up the map needs extra information, usually a time zone, which you must supply.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const instant = Temporal.Instant.from('2024-03-15T12:00:00Z');
// Up: an Instant needs a time zone to have a wall-clock reading
const zoned = instant.toZonedDateTimeISO('America/New_York');
console.log(zoned.toString()); // '2024-03-15T08:00:00-04:00[America/New_York]'
// Down: drop the zone, then narrow further
const wall = zoned.toPlainDateTime();
console.log(wall.toString()); // '2024-03-15T08:00:00'
console.log(wall.toPlainDate().toString()); // '2024-03-15'
console.log(wall.toPlainTime().toString()); // '08:00:00'
console.log(wall.toPlainDate().toPlainYearMonth().toString()); // '2024-03'
console.log(wall.toPlainDate().toPlainMonthDay().toString()); // '03-15'
// An Instant has no wall-clock fields at all
console.log(instant.year); // undefined
console.log(zoned.year); // 2024The last two lines show how the design protects you. An Instant cannot tell you its year, because the year can differ by time zone. Near midnight on December 31, it is already the next year in Tokyo and still the old year in Los Angeles.
The Same Wall Clock, Different Moments
A PlainDateTime is not a moment. The same wall-clock value maps to a different exact time in each zone:
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const nineAm = Temporal.PlainDateTime.from('2024-03-15T09:00');
const ny = nineAm.toZonedDateTime('America/New_York');
const tokyo = nineAm.toZonedDateTime('Asia/Tokyo');
console.log(ny.toInstant().toString()); // '2024-03-15T13:00:00Z'
console.log(tokyo.toInstant().toString()); // '2024-03-15T00:00:00Z'Both values read "9:00 on March 15". They are 13 hours apart on the timeline.
Choosing a Type
Ask these questions in order:
- Does the value describe something that happened, or will happen, at one moment for everyone? If you never need to show it in local terms, use
Instant. If you need local fields or calendar arithmetic in a specific place, useZonedDateTime. - Is the value a local reading that should stay the same wherever the user is? Use a Plain type. A birthday is March 15 in every country, so it is a
PlainDate. Turning it into an exact moment would shift it to March 14 for some users. - Which fields do you actually have? Pick the narrowest Plain type that holds them:
PlainTimefor an opening hour,PlainYearMonthfor a card expiry,PlainMonthDayfor an annual reminder. - Is it an amount rather than a point? Use
Duration.
Common Errors
- Using PlainDateTime for a cross-zone meeting. "2024-03-15T09:00" sent to a colleague in another zone has no single meaning. Store a
ZonedDateTime, or anInstantplus the organizer's zone. - Using Instant or ZonedDateTime for date-only data. Storing a due date as midnight UTC makes it display as the previous day west of Greenwich. Use
PlainDate. - Expecting implicit conversion. Temporal never falls back to the system time zone.
Temporal.PlainDate.from('2024-03-15T12:00Z')throws aRangeError, because aZstring describes an exact time and a PlainDate has no zone to interpret it in. Parse it as anInstantand convert it on purpose. - Treating Duration as a moment.
Durationhas no position on the timeline. To get a moment you add a duration to a date, time, or instant.
Exercise
An app stores (a) the time a user clicked "Pay", (b) a user's birthday, (c) a weekly team stand-up at 10:00 in Berlin, and (d) the opening hour of a shop, "08:30". Which Temporal type fits each?
Show answer
(a) Instant, because it is one exact moment and you only need ordering and storage. (b) PlainDate, because a birthday is the same calendar date in every zone. (c) ZonedDateTime with the Europe/Berlin zone, because the meeting must stay at 10:00 Berlin wall-clock time across daylight saving changes. (d) PlainTime, because it is a clock reading with no date or zone.
Exercise
Why does Temporal.Instant have no year or hour property, while Temporal.ZonedDateTime does?
Show answer
An Instant is only a position on the global timeline. Its year, day, and hour depend on which time zone you read it in, so it cannot give a single answer. A ZonedDateTime includes a time zone and calendar, so its wall-clock fields have one unambiguous value.
Exercise
A PlainDateTime of 2024-03-15T09:00 is converted to ZonedDateTime in America/New_York and in Asia/Tokyo. How many hours apart are the resulting instants, and which comes first?
Show answer
13 hours apart. Tokyo's 09:00 (UTC+9) is 00:00Z and New York's 09:00 (UTC-4 under daylight saving) is 13:00Z, so the Tokyo instant comes first.