Sub topic 17
Looking Ahead: Temporal and Checking Runtime Support
This chapter assumes you have read the earlier chapters on Date, in particular those on local time versus UTC, date arithmetic, and daylight saving time (DST). You should also be comfortable running code in Node.js or a browser console.
Temporal is a newer built-in date-time API for JavaScript. It was designed to fix the problems this tutorial has worked around: mutable objects, zero-based months, inconsistent parsing, and arithmetic that breaks across DST changes. Browsers and runtimes are adding support for it in stages, so your code has to check whether Temporal exists before using it.
What Temporal Changes
A Date wraps one value and leaves you to interpret it. Temporal provides separate types for separate meanings.
| Need | With `Date` | With Temporal |
|---|---|---|
| An exact instant | `Date` (ms since epoch) | `Temporal.Instant` (nanoseconds) |
| A calendar date with no time | Workarounds with UTC midnight | `Temporal.PlainDate` |
| A wall-clock time in a named zone | Not possible; only local or UTC | `Temporal.ZonedDateTime` |
| A length of time | Raw milliseconds | `Temporal.Duration` |
| Month numbering | 0–11 | 1–12 |
| Changing a value | Setters mutate in place | Methods return new objects |
Temporal also refuses ambiguous input. Temporal.PlainDate.from("not a date") throws a RangeError. It does not return an invalid object the way new Date("not a date") does.
Checking Runtime Support
Firefox shipped Temporal first, and other engines have been following. Support still differs across browser and Node.js versions. Detect the feature directly rather than relying on version numbers:
const hasTemporal = typeof globalThis.Temporal === "object";
console.log(hasTemporal ? "Temporal is available" : "Temporal is not available");When Temporal is missing, you can load a polyfill such as @js-temporal/polyfill, which exports a Temporal object with the same API. You can also keep using Date plus Intl as shown in earlier chapters.
Temporal in Action
This example only runs where Temporal exists. It shows 1-based months, values that never change, month-end clamping, and arithmetic that handles DST:
if (typeof globalThis.Temporal === "object") {
const { Temporal } = globalThis;
const jan31 = Temporal.PlainDate.from("2024-01-31");
console.log(jan31.month); // 1 (January is 1)
console.log(jan31.add({ months: 1 }).toString()); // "2024-02-29" (clamped, no overflow)
console.log(jan31.toString()); // "2024-01-31" (unchanged)
// DST starts in New York on 2024-03-10
const start = Temporal.ZonedDateTime.from("2024-03-09T12:00[America/New_York]");
console.log(start.add({ days: 1 }).toString()); // "2024-03-10T12:00:00-04:00[America/New_York]"
console.log(start.add({ hours: 24 }).toString()); // "2024-03-10T13:00:00-04:00[America/New_York]"
} else {
console.log("Temporal not available in this runtime");
}Adding { days: 1 } keeps the same wall-clock time. Adding { hours: 24 } adds exact elapsed time, which lands an hour later on the clock because the clocks moved forward. With Date, you would have to manage this difference yourself.
Moving Between Date and Temporal
Epoch milliseconds connect the two APIs:
if (typeof globalThis.Temporal === "object") {
const { Temporal } = globalThis;
const legacy = new Date("2024-07-04T16:30:00Z");
const instant = Temporal.Instant.fromEpochMilliseconds(legacy.getTime());
console.log(instant.toString()); // "2024-07-04T16:30:00Z"
console.log(new Date(instant.epochMilliseconds).toISOString()); // "2024-07-04T16:30:00.000Z"
}Temporal objects format through Intl too. toLocaleString and Intl.DateTimeFormat accept them, so the formatting skills from earlier chapters still apply.
Exercise
A ZonedDateTime is 2024-03-09T12:00 in America/New_York. Why do add({ days: 1 }) and add({ hours: 24 }) give different results, and which should a "same time tomorrow" reminder use?
Show answer
DST begins on 2024-03-10, so that calendar day is only 23 hours long. Adding one day keeps the wall-clock time at 12:00. Adding 24 exact hours lands at 13:00. A "same time tomorrow" reminder should use add({ days: 1 }).
Exercise
Write a one-line check that logs true only when the runtime has native Temporal.
Show answer
console.log(typeof globalThis.Temporal === "object");