Sub topic 17
Looking Ahead: Temporal and Checking Runtime Support
This chapter assumes you have worked through the earlier chapters. You should know how Date stores a millisecond time value, how local and UTC getters differ, why daylight saving time (DST) breaks naive arithmetic, and how Intl.DateTimeFormat displays an instant in an IANA time zone. Here you will see what Temporal changes, how its types relate to what you already know, and how to check whether your runtime supports it before you rely on it.
What Temporal Is
Temporal is a newer built-in date-time API, exposed as a global namespace object called Temporal. It is designed to replace Date for new code. Date does not go away, and existing code keeps working. Temporal was developed through the TC39 standards process and has been shipping in browser engines gradually. Support still differs between browsers, Node.js versions, and other runtimes, so check a current compatibility table such as MDN's before you depend on it in production.
Temporal fixes the problems this tutorial has warned you about:
| `Date` problem | Temporal behavior |
|---|---|
| Months are zero-based | Months are 1-based (`month: 1` is January) |
| Setters mutate in place | All objects are immutable; methods return new objects |
| One type stands for both "instant" and "calendar date" | Separate types for instants, wall-clock dates, times, and durations |
| Only local zone and UTC are built in | Any IANA time zone is part of the value |
| Loose, engine-dependent string parsing | Strict ISO 8601 / RFC 9557 parsing that throws on bad input |
| Invalid input gives an Invalid Date (`NaN`) | Invalid input throws a `RangeError` |
| Overflow silently rolls into the next month | Overflow is constrained or rejected, as you choose |
| No duration type | `Temporal.Duration` with calendar-aware arithmetic |
The Core Types
Temporal separates concepts that Date mixes together. Choosing a type is the first decision you make.
Temporal.Instantis an exact moment on the timeline, with nanosecond precision and no time zone. It is the closest match to aDatetime value.Temporal.ZonedDateTimeis an instant plus an IANA time zone and a calendar. Use it when DST-correct arithmetic in a real place matters.Temporal.PlainDate,Temporal.PlainTime, andTemporal.PlainDateTimeare wall-clock values with no time zone: a birthday, an opening hour, or a meeting written down before anyone knows where it will happen.Temporal.PlainYearMonthandTemporal.PlainMonthDaycover partial dates such as "March 2025" or "every July 4".Temporal.Durationis a length of time, such as 2 months and 14 days.Temporal.Nowreads the current instant, time zone, and date.
Checking Runtime Support
Temporal is a global, so a typeof check detects it. If the runtime lacks it, you can load a polyfill such as @js-temporal/polyfill from npm. Load it only when needed so that native implementations win.
async function loadTemporal() {
if (typeof globalThis.Temporal !== "undefined") {
return globalThis.Temporal; // native support
}
const mod = await import("@js-temporal/polyfill"); // npm install @js-temporal/polyfill
return mod.Temporal;
}
const Temporal = await loadTemporal(); // top-level await: run as an ES module (.mjs)
console.log(Temporal.Now.instant().toString()); // e.g. "2026-11-24T10:15:04.334123456Z"
console.log(Temporal.Now.timeZoneId()); // the runtime's zone, e.g. "Europe/Berlin"Immutability and 1-Based Months
The month-end overflow trap from the arithmetic chapter turns into explicit, predictable behavior:
const jan31 = Temporal.PlainDate.from("2024-01-31");
console.log(jan31.month); // 1 (January is 1)
const next = jan31.add({ months: 1 });
console.log(next.toString()); // "2024-02-29" (constrained to month end)
console.log(jan31.toString()); // "2024-01-31" (original unchanged)
try {
jan31.add({ months: 1 }, { overflow: "reject" });
} catch (err) {
console.log(err instanceof RangeError); // true
}
const changed = jan31.with({ day: 15 }); // "with" replaces fields, returning a new object
console.log(changed.toString()); // "2024-01-15"The default overflow: "constrain" clamps to the nearest valid date. "reject" throws instead, which suits validating user input.
Strict Parsing
Temporal only accepts ISO 8601 and its RFC 9557 extension, which adds a bracketed time zone such as [America/New_York]. It does not guess.
console.log(Temporal.PlainDate.from("2024-03-10").toString()); // "2024-03-10"
try {
Temporal.PlainDate.from("03/10/2024");
} catch (err) {
console.log(err.name); // "RangeError"
}The date-only versus date-time ambiguity also goes away. A PlainDate string never becomes an instant, so it cannot quietly be read as UTC midnight.
DST-Correct Arithmetic with ZonedDateTime
In the DST chapter you saw that "one day later" and "24 hours later" can differ. Temporal makes you say which one you mean:
const zdt = Temporal.ZonedDateTime.from("2024-03-09T12:00[America/New_York]");
console.log(zdt.add({ days: 1 }).toString());
// "2024-03-10T12:00:00-04:00[America/New_York]" same wall-clock time, next day
console.log(zdt.add({ hours: 24 }).toString());
// "2024-03-10T13:00:00-04:00[America/New_York]" exactly 24 elapsed hoursClocks in New York moved forward on March 10, 2024. Adding days keeps the wall-clock time. Adding hours keeps elapsed time. With Date you had to simulate this by hand with UTC setters.
Durations and Comparison
until and since return a Duration. largestUnit controls how the result is balanced.
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"
const dates = ["2024-05-01", "2023-12-25", "2024-01-10"].map((s) => Temporal.PlainDate.from(s));
dates.sort(Temporal.PlainDate.compare);
console.log(dates.map(String)); // ["2023-12-25", "2024-01-10", "2024-05-01"]Working with Date and Intl
You will mix Temporal with existing Date code for years. The bridge between them is epoch milliseconds:
const legacy = new Date("2024-07-04T16:30:00Z");
const instant = Temporal.Instant.fromEpochMilliseconds(legacy.getTime());
const inTokyo = instant.toZonedDateTimeISO("Asia/Tokyo");
console.log(inTokyo.toString()); // "2024-07-05T01:30:00+09:00[Asia/Tokyo]"
const backToDate = new Date(instant.epochMilliseconds);
console.log(backToDate.getTime() === legacy.getTime()); // trueGoing from Temporal back to Date truncates nanoseconds to milliseconds. Everything you learned about Intl still applies. Temporal objects have toLocaleString, and in supporting engines Intl.DateTimeFormat#format accepts Instant and the Plain* types:
const birthday = Temporal.PlainDate.from("2024-07-04");
console.log(birthday.toLocaleString("en-US", { dateStyle: "long" })); // "July 4, 2024"
const meeting = Temporal.ZonedDateTime.from("2024-07-04T09:00[Europe/Paris]");
console.log(meeting.toLocaleString("en-GB", { timeStyle: "short", timeZoneName: "short" }));
// e.g. "09:00 CEST"A ZonedDateTime already carries its own zone. Passing one to Intl.DateTimeFormat#format throws a TypeError, so call its toLocaleString instead. Formatting a PlainDate never shifts it into another day, because it has no time zone to convert from.
Deciding When to Adopt It
- New code where Temporal is native in all target runtimes: use Temporal for calendar logic, scheduling, and durations.
- Mixed or older targets: use a polyfill if you can afford the bundle size, or keep
Dateand apply the UTC and copying rules from earlier chapters. - Storage and APIs: keep sending ISO 8601 strings or epoch milliseconds. Both formats work with
Dateand Temporal alike. For values that need a zone, Temporal's bracketed RFC 9557 strings round-trip it. - Migration: convert at boundaries with
Instant.fromEpochMillisecondsandepochMilliseconds, and move modules over one at a time.
Exercise
A recurring reminder is set for 09:00 every day in Europe/Berlin. Using Temporal, should you compute the next reminder with add({ days: 1 }) or add({ hours: 24 }) on a ZonedDateTime, and why?
Show answer
Use add({ days: 1 }). It keeps the wall-clock time at 09:00 across DST changes. add({ hours: 24 }) adds exact elapsed time, so the reminder would drift to 08:00 or 10:00 on the day the clocks change.
Exercise
Write a function isoDateIsValid(text) that returns true only for a real calendar date in YYYY-MM-DD form (so "2023-02-29" is false), using Temporal and no regular expression.
Show answer
function isoDateIsValid(text) { try { Temporal.PlainDate.from(text, { overflow: "reject" }); return true; } catch { return false; } } Strict parsing rejects non-ISO strings, and overflow: "reject" rejects out-of-range fields such as February 29 in a non-leap year. Without the option, the default "constrain" would clamp the day instead of failing.
Exercise
Your code runs in a browser where typeof Temporal is "undefined". Name two ways to proceed, and say which value you would use to pass a moment between legacy Date code and Temporal code.
Show answer
Load a polyfill such as @js-temporal/polyfill, ideally only when the native global is missing, or keep using Date with the UTC and copy-before-mutate practices. Pass moments as epoch milliseconds, using date.getTime() with Temporal.Instant.fromEpochMilliseconds(), and instant.epochMilliseconds with new Date().