Skip to the solution
HardFrontend6 min read

How does the Temporal API fix JavaScript Date — Instant, PlainDate and ZonedDateTime?

437 views

The problem

Question presented to candidate: "If you write const d = new Date(2024, 0, 31); d.setMonth(1);, does that genuinely change d in place, and what does d actually become? How would the Temporal equivalent behave differently?"

What a strong answer should cover:

  • 📌 Interview term: the real, direct answer to the prompt — verified directly, in a real, current browser (Temporal is not yet available in Node, confirmed directly): the legacy Date object is genuinely mutable — d.setMonth(1) genuinely changes d in place, and every other variable referencing the SAME object sees that change too. Temporal objects are genuinely immutable — an operation like .add() genuinely returns a brand-new object, leaving the original completely untouched, verified directly.
  • 📌 Interview term: Temporal.PlainDate — represents a calendar date with genuinely no time-of-day and no timezone at all — verified directly, a PlainDate genuinely has no hour property — the correct type for something like a birthday, which is the same calendar date everywhere in the world.
  • 📌 Interview term: Temporal.ZonedDateTime — represents a real moment in time tied to a specific, real IANA timezone (like America/New_York), genuinely tracking the real UTC offset for that zone.
  • 📌 Interview term: Temporal.Instant — represents a single, absolute point in time, with no calendar or timezone attached at all — the closest Temporal type to a raw timestamp.
  • A precise answer names the real, direct fix for legacy Date's famous zero-indexed-month footgun: verified directly, new Date(2024, 0, 15)'s month is genuinely 0 for January, while Temporal.PlainDate's month property is genuinely 1 for January — a real, deliberate, verified correction.

Clarifying questions expected:

  • None — this is a definitional/comparison question; directly answering the prompt's own mutation question with real, verified proof is the strong signal.

Code / implementation expected: Yes — real, direct proof of Date's mutability versus Temporal's immutability, plus the month-indexing contrast, verified directly in a real browser (since Temporal is not yet in Node).

  • temporal
  • es2024
  • date

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read · 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof (verified in a real, current browser — Temporal is not yet in Node): Date genuinely mutates in place while Temporal is genuinely immutable, plus the real month-indexing fix

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 150 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How do iterators and the iterable protocol work?

Continue