Under The Hood
frontend javascript Temporal proposal (Stage 3, ECMA-262)

Modern Temporal API: Immutable Calendar Math vs Mutable Date

Last updated
Prerequisites:
JavaScript Date basics
Understanding of timezones and UTC
  • temporal
  • date
  • intl
  • timezone
  • calendar
  • immutable

Read at your depth

The practical view

Temporal provides new date/time types: Temporal.Instant (a precise UTC moment), Temporal.PlainDate, PlainTime, PlainDateTime (wall-clock values without timezone), Temporal.ZonedDateTime (moment + timezone), and Temporal.Duration. They are immutable: every operation returns a NEW instance — d.add({ days: 1 }) returns a new date, leaving d unchanged (unlike Date.setDate which mutates). Temporal.Now.zonedDateTimeISO() gets the current moment. Math like .add, .subtract, .until, .since works with calendar-aware durations. Dates compare with plain <, >, and .equals. This replaces the error-prone mutable Date API for real applications.

The same idea in other frameworks

react equivalent
React apps still use Date + libraries like date-fns; Temporal offers a future native alternative for scheduling UIs.

Legacy vs modern

Adding a day: mutable Date.setDate vs immutable Temporal

Date mutates in place and is easy to corrupt with DST/overflow; Temporal returns a new immutable value with calendar-safe arithmetic.

before → after
Mutable Date arithmetic
const d = new Date(); // ❌ OLD: mutable
const t = d.setDate(d.getDate() + 1); // mutates d in place — original lost
Immutable Temporal arithmetic
const t = Temporal.Now.zonedDateTimeISO(); // ✅ NEW: immutable
const next = t.add({ days: 1 }); // returns a NEW value; t unchanged

Interview gotchas

Context

Interviewers use this to test whether you understand the conceptual conflation in Date (instant vs wall-clock vs timezone) that causes real production bugs.

The mechanical answer

Date conflates three distinct concepts: an absolute instant, a local wall-clock reading, and a timezone interpretation. Its methods mutate in place, work in milliseconds (losing sub-ms precision), and arithmetic like setDate is calendar- and DST-unaware — adding 24h across a DST transition changes the wall clock unexpectedly, and adding '1 month' is undefined. Temporal splits these into Instant, PlainDate/PlainTime/PlainDateTime, and ZonedDateTime, all immutable with nanosecond precision. Arithmetic is done through calendar and timezone algorithms (with explicit overflow rules for month-ends and DST gaps), so the results are deterministic and reproducible.

Trap

The naive answer is 'Date is fine if you just use UTC everywhere'. UTC-only breaks local calendar features and still leaves you with mutable in-place mutation bugs. Another trap: assuming Temporal is just 'Date with better method names' — it is a different type system (separate types for instants vs wall clocks), so adopting it means restructuring how you model time, not swapping one call for another.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.