Sub topic 1
Why Temporal Exists: What Goes Wrong with Date
JavaScript's Date object came from a quick port of Java's java.util.Date in 1995. Java replaced that class years ago. JavaScript kept it. Temporal is the replacement: a set of new types built into the language for dates, times, time zones, and durations. This chapter covers the problems with Date that Temporal fixes, and how to run Temporal code today.
Prerequisites
This chapter assumes you know:
- Modern JavaScript syntax (ES2015+), including
const, arrow functions, and modules - Objects, methods, and exceptions
- The legacy
Dateobject at a basic level - How to run JavaScript in Node.js or a browser
- How to install npm packages and import them with ES modules
What Goes Wrong with Date
Mutation and zero-based months
A Date changes in place. Every variable that points to it sees the change. Months are also counted from 0, and setters roll invalid dates forward without telling you.
const due = new Date(2024, 0, 31); // January 31 (months start at 0)
const reminder = due; // same object, not a copy
reminder.setMonth(1); // try to move to February
console.log(due.getMonth(), due.getDate()); // 2 2 -> March 2, and `due` changed tooFebruary 31 does not exist, so Date quietly rolls over to March 2. Because reminder is the same object as due, the bug spreads to every other place that uses due.
Inconsistent parsing
Date strings that look alike are parsed in different time zones:
console.log(new Date('2024-03-15').toISOString()); // '2024-03-15T00:00:00.000Z' (read as UTC)
console.log(new Date('2024-03-15T00:00').getHours()); // 0 (read as local time)A date-only string is read as UTC midnight. In New York, that is the evening of March 14, so a birthday can show up one day early.
Other gaps
| Problem | Effect |
|---|---|
| Only two time zones, local and UTC | You can't ask what time it is in Tokyo without a library |
| No type for a date alone or a time alone | A birthday has to carry a fake time and time zone |
| No duration type | "3 months" becomes hand-written millisecond math |
| Daylight saving time ignored in arithmetic | Adding 24 hours of milliseconds is not the same as adding one day |
| Gregorian calendar only | Hebrew, Islamic, or Japanese calendars need extra code |
Temporal answers each row with immutable values, months counted from 1, strict ISO 8601 parsing, full IANA time zone support, a Duration type, and pluggable calendars.
Running Temporal Today: Native Support and Polyfills
Temporal is shipping natively in more and more engines. Firefox and Chrome include it, but other browsers, Node.js versions, and server runtimes may not have it yet. Check a current compatibility table such as MDN's before you rely on the global Temporal.
Where native support is missing, use a polyfill. A polyfill is a library that implements the API in plain JavaScript. The reference polyfill is @js-temporal/polyfill. The smaller temporal-polyfill package is another option.
npm install @js-temporal/polyfillThe pattern below uses the native global when it exists and falls back to the polyfill otherwise:
// Use the native global when available, otherwise the polyfill
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
console.log(Temporal.Now.plainDateISO().toString());A static import always downloads the polyfill, even when it isn't needed. To load it only when needed, use a dynamic import with top-level await:
const Temporal =
globalThis.Temporal ?? (await import('@js-temporal/polyfill')).Temporal;
console.log(Temporal.Now.instant().toString());Exercise
A user in New York runs new Date('2024-07-04').getDate() and gets 3. Why?
Show answer
A date-only ISO string is parsed as midnight UTC. In New York (UTC-4 in July), that moment is 8 p.m. on July 3 local time, and getDate() reports the local day.
Exercise
Why can loading the polyfill with a dynamic import be better than the static import version?
Show answer
The static import always downloads and evaluates the polyfill. The dynamic import runs only when globalThis.Temporal is missing, so runtimes with native support skip the extra code.