JavaScript Tutorial 0/230 lessons ~6 min read Lesson 21

    JS Temporal

    Temporal is the Stage-3 replacement for Date.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Temporal is the Stage-3 replacement for Date. It separates wall-clock concepts — PlainDate, PlainTime, ZonedDateTime, Instant, Duration — so calendar math and time zones stop depending on mutable, timezone-ambiguous Date objects. As of late 2026 it still needs a polyfill in most production apps (@js-temporal/polyfill); Firefox ships a partial implementation behind a preference.

    Why this feature exists

    Why Temporal exists: Date conflates instants with local calendar fields, is mutable, and parses strings inconsistently across engines. Temporal makes each concept a separate immutable type.

    • Instant — a unique point on the UTC timeline (like a Unix epoch, but typed).
    • PlainDate / PlainDateTime — calendar fields with no time zone.
    • ZonedDateTime — calendar fields plus an IANA zone (America/New_York).
    • Duration — a signed amount of years/months/days/hours/… used with add, since, and until.

    Best practices

    • Prefer PlainDate for birthdays and billing cycles; use ZonedDateTime for meetings.
    • Always pass IANA zones — never rely on the host's local offset alone.
    • Ship @js-temporal/polyfill (or equivalent) until engines enable Temporal by default.

    Interview questions

    How is Temporal.PlainDate different from Date?(Intermediate)

    PlainDate stores only calendar year/month/day with no time zone and no time-of-day. Date always represents an instant in time and exposes local getters that depend on the host zone, which is why "add one month" and DST edges are error-prone with Date.

    Is Temporal available in Node and Chrome without a polyfill in 2026?(Beginner)

    Not broadly. Node 22 still has no global Temporal; browsers are rolling it out behind flags or partially. Production apps should depend on a maintained polyfill and feature-detect before use.

    Hands-on lab

    Lab: In the playground below, compute the number of calendar days between two fixed PlainDates and print a ZonedDateTime two hours later in Europe/London. Confirm the Output block matches when you use the polyfill locally.

    Examples

    Verified with @js-temporal/polyfill 0.5.x (Node 22 has no built-in Temporal yet). Note the correct total({ unit: "day" }) form — a bare string unit is invalid:

    js
    import { Temporal } from "@js-temporal/polyfill";
    const d1 = Temporal.PlainDate.from("2026-05-01");
    const d2 = Temporal.PlainDate.from("2026-08-15");
    const days = d2.since(d1).total({ unit: "day" });
    console.log("days=" + days);
    const start = Temporal.ZonedDateTime.from("2026-11-21T10:00:00[America/New_York]");
    const end = start.add({ hours: 2, minutes: 30 });
    console.log("start=" + start.toString());
    console.log("end=" + end.toString());
    console.log("duration=" + start.until(end).toString());
    const nextBill = Temporal.PlainDate.from("2026-11-21").add({ months: 1 });
    console.log("nextBill=" + nextBill.toString());

    Output

    days=106
    start=2026-11-21T10:00:00-05:00[America/New_York]
    end=2026-11-21T12:30:00-05:00[America/New_York]
    duration=PT2H30M
    nextBill=2026-12-21

    In browsers without Temporal, load the polyfill before this code. Do not call Temporal.Now.plainDateISO() in golden tests — the result changes every day; prefer fixed PlainDate.from(...) values.

    Common pitfalls

    • Calling .total("days") — the API requires .total({ unit: "day" }).
    • Mutating Temporal values — they are immutable; add returns a new instance.
    • Mixing Date and Temporal without an explicit conversion at the boundary.

    Try it yourself

    Edit the JS panel and press Run — profile in Chrome DevTools Performance and Memory panels.

    Try it yourself

    Preview

    Summary

    Temporal makes calendar and time-zone math explicit. Learn PlainDate, ZonedDateTime, and Duration, verify with a polyfill, and keep Date only at legacy boundaries.

    Key takeaways

    • Temporal splits Date's mixed concepts into immutable types.
    • Use total({ unit: "day" }) — not a bare string.
    • Polyfill until engines ship Temporal by default.
    Ready to mark this lesson complete?Track your journey across the entire course.