← Back to JavaScript, page 5

javascript-0093 · Hard senior

What is the Temporal API (new), and how does it fix the problems of Date? Where is it available?

Temporal is a new built-in date and time API with separate immutable types for exact instants, calendar dates, wall-clock times, zoned date-times, and durations. It reached TC39 Stage 4 in March 2026 and is scheduled for ECMAScript 2027, and shipped in Firefox 139 and Chrome 144, while Node.js 24 needs a flag. It fixes Date's mutability, zero-based months, weak time zone support, and unclear parsing.

Full answer

Date mixes several ideas in one mutable object. Temporal separates them into types that say what the value means:

  • Temporal.Instant is an exact point in time (a timestamp), without a time zone.
  • Temporal.PlainDate, PlainTime, and PlainDateTime are calendar and wall-clock values without any zone, such as a birthday or an opening time.
  • Temporal.ZonedDateTime is a wall-clock time in a named time zone, including daylight saving rules.
  • Temporal.Duration is an amount of time, such as one month and three days.
  • Temporal.Now gives the current time in these types.

Key improvements: all objects are immutable, so add and with return new objects. Months run from 1 to 12. Parsing accepts ISO 8601 strings only, with predictable results. Arithmetic is calendar-aware: adding one month to 31 January either clamps to the end of February or rejects, depending on the overflow option, instead of silently jumping into March. A ZonedDateTime knows about daylight saving, so adding one day keeps the same wall-clock time, while adding 24 hours moves by exact time. Comparison uses compare, and differences come from until and since with a chosen largest unit. Non-Gregorian calendars are supported through calendar options.

Status (labelled new): Temporal reached Stage 4 at the TC39 meeting in March 2026 and is planned for ECMAScript 2027. It is available in Firefox 139 and later and in Chrome and Edge 144 and later. Safari support was in a 27.3 beta at the time of writing, and Node.js 24 requires the --harmony-temporal flag. For older environments, use a polyfill and feature-detect with typeof Temporal !== 'undefined'. Check current support before relying on it in production.

For storing data, send Instant values as ISO strings with a Z, or store a ZonedDateTime with its zone name when the local meaning matters, such as a meeting that stays at 9 a.m. in its zone.

javascript
if (typeof Temporal === 'undefined') throw new Error('Temporal is not available in this runtime');

const lastDay = Temporal.PlainDate.from('2026-01-31');
console.log(lastDay.add({ months: 1 }).toString(), '(clamped to the end of February)');
try {
  lastDay.add({ months: 1 }, { overflow: 'reject' });
} catch (error) {
  console.log('overflow reject:', error.name);
}

const start = Temporal.PlainDate.from('2026-01-01');
const end = Temporal.PlainDate.from('2026-03-01');
console.log(start.until(end).toString(), start.until(end, { largestUnit: 'month' }).toString(), start.until(end).days);
console.log(Temporal.PlainDate.compare(start, end), start.month, start.dayOfWeek, start.daysInMonth);

const before = Temporal.ZonedDateTime.from('2026-03-07T09:00:00[America/New_York]');
console.log('plus one day:  ', before.add({ days: 1 }).toString({ timeZoneName: 'never' }));
console.log('plus 24 hours: ', before.add({ hours: 24 }).toString({ timeZoneName: 'never' }));

const instant = Temporal.Instant.from('2026-01-15T10:30:00Z');
console.log(instant.toString(), instant.epochMilliseconds);
console.log(instant.toZonedDateTimeISO('Asia/Kolkata').toString({ timeZoneName: 'never' }));
console.log(instant.toZonedDateTimeISO('America/New_York').toString({ timeZoneName: 'never' }));

const original = Temporal.PlainDate.from('2026-05-05');
const changed = original.with({ day: 20 });
console.log(original.toString(), changed.toString(), original === changed);

console.log(Temporal.Duration.from({ hours: 25 }).round({ largestUnit: 'day' }).toString());
console.log(Temporal.PlainDateTime.from('2026-05-05T10:00').toZonedDateTime('Europe/London').epochMilliseconds);
try {
  Temporal.PlainDate.from('2026-02-30', { overflow: 'reject' });
} catch (error) {
  console.log('invalid date:', error.name);
}

Run with Node.js v24.22.1 using the --harmony-temporal flag as js-93.js; two runs gave identical output. Output: 2026-02-28 (clamped to the end of February) overflow reject: RangeError P59D P2M 59 -1 1 4 31 plus one day: 2026-03-08T09:00:00-04:00 plus 24 hours: 2026-03-08T10:00:00-04:00 2026-01-15T10:30:00Z 1768473000000 2026-01-15T16:00:00+05:30 2026-01-15T05:30:00-05:00 2026-05-05 2026-05-20 false P1DT1H 1777971600000 invalid date: RangeError

Key points

  • Temporal has separate immutable types: Instant, Plain types, ZonedDateTime, and Duration.
  • Months are 1 to 12; parsing is ISO-only and strict.
  • Calendar-aware arithmetic with an overflow option; daylight saving handled by ZonedDateTime.
  • Stage 4 in March 2026, planned for ECMAScript 2027; ships in Firefox 139 and Chrome 144.
  • Node.js 24 needs a flag and Safari support is only in a beta, so feature-detect and polyfill.

Common mistakes

  • Using Temporal in production without checking browser and runtime support or adding a polyfill.
  • Using PlainDateTime for a moment in time that must be exact, which has no zone and no instant.
  • Adding 24 hours when one calendar day was meant, around a daylight saving change.
  • Storing local times without the time zone name when the zone matters.

Follow-ups

  • When would you store an Instant, and when a ZonedDateTime?
  • How does Temporal handle a wall-clock time that does not exist or occurs twice during daylight saving changes?
  • How would you feature-detect and load a polyfill only when needed?

Tips

  • List the types and what each one means; the idea of "say what the value is" is the core.
  • Show the month overflow and the daylight saving day versus 24 hours examples.
  • Label the status honestly: new, with limited support, so check support and polyfill.