Skip to content
jshacks.dev
Fundamentals

Getting Started With the Temporal API in JavaScript

Temporal replaces Date with separate types for calendar dates, exact instants, zoned times and durations. See the types, DST arithmetic and moving off Date.

8 min read
Summarize in AI
Flat illustration of a browser and a server trading a request and a response, with the JavaScript, NGINX, Postman, Swagger and GraphQL logos.

The Date object that JavaScript shipped with in 1995 mixes up four different ideas: a calendar date, a time of day, an exact moment and a time zone. Its months count from zero, it mutates in place, and it has no time zones beyond "local" and "UTC". Temporal is the replacement that the standards committee has spent years on, and it splits the ideas into separate types so that code says what it means. This post introduces the types and the operations you will use most, with every output taken from a run on Node.js 24 with the --harmony-temporal flag, since that version does not enable the API by default. Check typeof Temporal on your runtime: current Firefox and Chromium releases ship it, and a polyfill covers the rest. The older object and its traps are covered in the post on Date pitfalls, and what is still coming is in the post on ES2027 and Temporal.

One type per idea

Pick the type by asking what the value means, and the right operations follow.

TypeMeansExample
Temporal.PlainDateA calendar date with no time or zoneA birthday, a due date
Temporal.PlainTimeA time of day with no dateAn opening time
Temporal.PlainDateTimeA date and a time with no zoneA form's date and time fields
Temporal.InstantAn exact moment on the timelineA log timestamp
Temporal.ZonedDateTimeAn exact moment in a time zoneA meeting in New York
Temporal.DurationA length of timeNinety minutes

Plain dates

PlainDate is created from numbers or an ISO string, months start at 1 and the object never changes.

JavaScript
const date = Temporal.PlainDate.from("2026-05-06");
const fromParts = new Temporal.PlainDate(2026, 5, 6);

console.log(date.toString(), fromParts.equals(date));
console.log(date.year, date.month, date.day, date.dayOfWeek, date.daysInMonth, date.inLeapYear);

const next = date.add({ days: 30 });
console.log(next.toString(), date.toString());
console.log(date.with({ day: 1 }).toString());
console.log(date.toLocaleString("en-US", { dateStyle: "long" }));
Text
2026-05-06 true
2026 5 6 3 31 false
2026-06-05 2026-05-06
2026-05-01
May 6, 2026

The month is 5 for May, and dayOfWeek is 1 for Monday through 7 for Sunday. add() returned a new date and left the original alone, and with() changed one field of a copy. Because the value is a calendar date, there is no time zone to shift it, which is exactly what you want for a birthday: it stays the same date in Tokyo and in Lima, where a Date set to midnight can fall on the previous day.

Invalid dates and month arithmetic

Temporal has an explicit rule for dates that do not exist. Objects constrain by default, and you can ask it to reject.

JavaScript
const january31 = Temporal.PlainDate.from("2026-01-31");

console.log(january31.add({ months: 1 }).toString());
console.log(january31.add({ months: 1 }, { overflow: "constrain" }).toString());

try {
  january31.add({ months: 1 }, { overflow: "reject" });
} catch (error) {
  console.log(error.name);
}

console.log(Temporal.PlainDate.from({ year: 2026, month: 2, day: 30 }).toString());

try {
  Temporal.PlainDate.from({ year: 2026, month: 2, day: 30 }, { overflow: "reject" });
} catch (error) {
  console.log(error.name);
}

try {
  Temporal.PlainDate.from("2026-02-30");
} catch (error) {
  console.log(error.name);
}
Text
2026-02-28
2026-02-28
RangeError
2026-02-28
RangeError
RangeError

Adding one month to January 31 gave February 28, the last valid day, because the default behavior clamps. With overflow: "reject" the same call throws a RangeError, so a billing system that must not silently shift a due date can say so. A string with an impossible date is always rejected. The legacy Date rolls February 30 forward to March 2, which is the kind of silent change this design removes.

Differences and durations

until() and since() return a Duration, and you choose the largest unit. round() rebalances a duration.

JavaScript
const start = Temporal.PlainDate.from("2026-01-01");
const end = Temporal.PlainDate.from("2026-03-15");

console.log(start.until(end).toString());
console.log(start.until(end, { largestUnit: "month" }).toString());
console.log(start.until(end, { largestUnit: "week" }).toString());
console.log(end.since(start).days);

const raw = Temporal.Duration.from({ hours: 1, minutes: 90 });
console.log(raw.toString());
console.log(raw.round({ largestUnit: "hour" }).toString());
console.log(raw.total({ unit: "minute" }));

const sorted = ["2026-03-01", "2026-01-15", "2026-02-20"]
  .map((s) => Temporal.PlainDate.from(s))
  .sort(Temporal.PlainDate.compare);
console.log(sorted.map(String));
Text
P73D
P2M14D
P10W3D
73
PT1H90M
PT2H30M
150
[ '2026-01-15', '2026-02-20', '2026-03-01' ]

The same gap reads as 73 days, as 2 months and 14 days, or as 10 weeks and 3 days, depending on largestUnit. A Duration keeps the units you gave it, so PT1H90M is not normalized until you ask, and total() converts to a single unit. Every Temporal type has a static compare function that returns -1, 0 or 1, which plugs straight into sort(), and equals() for equality. Comparing two Date objects with === compares identity, which is a different bug.

Instants and time zones

An Instant is an exact moment, stored as nanoseconds since the Unix epoch. It has no time zone. You convert it to a ZonedDateTime to view it in one.

JavaScript
const instant = Temporal.Instant.from("2026-05-06T07:08:09Z");

console.log(instant.toString());
console.log(instant.epochMilliseconds);
console.log(instant.toZonedDateTimeISO("Asia/Tokyo").toString());
console.log(instant.toZonedDateTimeISO("America/New_York").toString());
console.log(instant.toZonedDateTimeISO("UTC").hour);

const legacy = new Date(instant.epochMilliseconds);
console.log(legacy.toISOString());
console.log(Temporal.Instant.fromEpochMilliseconds(legacy.getTime()).equals(instant));
Text
2026-05-06T07:08:09Z
1778051289000
2026-05-06T16:08:09+09:00[Asia/Tokyo]
2026-05-06T03:08:09-04:00[America/New_York]
7
2026-05-06T07:08:09.000Z
true

One instant, three views: it was 16:08 in Tokyo, 03:08 in New York and 07:08 in UTC. The conversion to and from Date is two lines, which makes gradual migration possible: convert at the boundaries of the code you touch and leave the rest alone.

Daylight saving time

A ZonedDateTime applies the time zone rules, and Temporal makes the choice between "same wall clock" and "same elapsed time" explicit through the unit you add.

JavaScript
const before = Temporal.ZonedDateTime.from("2026-03-07T12:00:00[America/New_York]");

console.log(before.toString());
console.log(before.add({ days: 1 }).toString());
console.log(before.add({ hours: 24 }).toString());
console.log(before.add({ days: 1 }).offset, before.offset);
console.log(before.until(before.add({ days: 1 })).toString());
console.log(before.until(before.add({ days: 1 }), { largestUnit: "hour" }).toString());
console.log(before.hoursInDay);
console.log(before.add({ days: 1 }).hoursInDay);
Text
2026-03-07T12:00:00-05:00[America/New_York]
2026-03-08T12:00:00-04:00[America/New_York]
2026-03-08T13:00:00-04:00[America/New_York]
-04:00 -05:00
PT23H
PT23H
24
23

In New York, daylight saving began on March 8, 2026. Adding one day kept the wall clock at noon and the offset moved from -05:00 to -04:00, so only 23 hours passed. Adding 24 hours moved by exact elapsed time and landed at 1 p.m. The gap between the two zoned values is PT23H, and hoursInDay reads 24 on March 7 and 23 on March 8, which is the same fact seen from the day's side. This is the distinction that Date cannot express, and it is where scheduling code goes wrong: a "daily at noon" job that adds 24 hours drifts by an hour twice a year.

Time of day and combined values

PlainTime and PlainDateTime fill the gaps between a date and a full zoned value, and they convert upward when you supply the missing piece.

JavaScript
const time = Temporal.PlainTime.from("09:30");
const date = Temporal.PlainDate.from("2026-05-06");

const local = date.toPlainDateTime(time);
const zoned = local.toZonedDateTime("Europe/London");

console.log(time.add({ minutes: 45 }).toString());
console.log(local.toString());
console.log(zoned.toString());
console.log(zoned.toInstant().toString());
console.log(Temporal.PlainDate.from("2026-05-06T10:00:00+02:00[Europe/Berlin]").toString());
Text
10:15:00
2026-05-06T09:30:00
2026-05-06T09:30:00+01:00[Europe/London]
2026-05-06T08:30:00Z
2026-05-06

A form with separate date and time fields maps to the plain types, and the time zone is attached last, when the user's intention becomes an exact moment. The final line shows that parsing a zoned string into a PlainDate keeps only the calendar date it names, with no time-zone shift.

Parsing and storing

Temporal parses ISO strings strictly and throws a RangeError on anything else, which makes it a good validator for user input. Store the ISO string, since every type round-trips through toString() and from(), and pick the type to store by the meaning of the data: a due date as a PlainDate string, an event time as an Instant string plus the zone ID when the local time matters.

JavaScript
for (const text of ["2026-05-06", "2026-13-01", "06/05/2026", "2026-5-6"]) {
  try {
    console.log(text.padEnd(12), Temporal.PlainDate.from(text).toString());
  } catch (error) {
    console.log(text.padEnd(12), error.name);
  }
}

const zone = "America/Los_Angeles";
const meeting = Temporal.ZonedDateTime.from(`2026-11-02T09:00:00[${zone}]`);
const stored = { at: meeting.toInstant().toString(), zone };
const restored = Temporal.Instant.from(stored.at).toZonedDateTimeISO(stored.zone);

console.log(stored);
console.log(restored.equals(meeting), restored.toString());
Text
2026-05-06   2026-05-06
2026-13-01   RangeError
06/05/2026   RangeError
2026-5-6     RangeError
{ at: '2026-11-02T17:00:00Z', zone: 'America/Los_Angeles' }
true 2026-11-02T09:00:00-08:00[America/Los_Angeles]

Only the ISO form parsed, and every other format failed with a RangeError and no guessing. The stored pair of an instant and a zone ID rebuilt the same zoned value. The zone ID is held in a variable of our own, because the property that reads it back from a zoned value was renamed while the API was being standardized: older builds, including the one used here, expose timeZone, and the final API uses timeZoneId. Storing the zone ID alongside the instant preserves the meaning "9 a.m. in Los Angeles", which a bare timestamp loses, and which matters when the zone's rules change before the event happens.

Migrating from Date

Start where the bugs are. Replace code that stores calendar dates as Date objects at midnight with PlainDate. Replace timestamps with Instant. Use ZonedDateTime for anything shown to a person in a time zone. Keep Date at the edges where libraries still expect it.

Date codeTemporal replacement
new Date().getTime()Temporal.Now.instant().epochMilliseconds
new Date(2026, 4, 6) (month is 0 based)new Temporal.PlainDate(2026, 5, 6)
d.setDate(d.getDate() + 30) (mutates)date.add({ days: 30 })
(b - a) / 86400000a.until(b, { largestUnit: "day" }).days
d.toISOString()instant.toString()

Where Temporal does not help yet

Support is the practical limit. Where the runtime does not ship it, a polyfill such as @js-temporal/polyfill provides it, at a size cost, and some UI libraries still require Date. Temporal also does not format for display on its own beyond toLocaleString(), which uses Intl.DateTimeFormat underneath, as covered in the post on the Intl API. And a calendar system other than ISO needs the calendar argument, which is outside the scope of this introduction.

For a script that handles one fixed date and one time zone, Date is still adequate. The benefit of Temporal grows with the number of time zones, calendar arithmetic and persisted dates in your system.

The API is specified in the Temporal proposal, and MDN documents each type on its Temporal reference page.

Also inAPIs
Taggedes2027
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.