JavaScript Temporal API: replace Date in real code

Authors
  • avatar
    Name
    Hamza Rahman
Published on
-
11 mins read

Date causes bugs with calendar dates, time zones, daylight saving time (DST), durations, sorting, and values that cross an API or database boundary. This guide shows how to replace it with Temporal for each of those jobs, and where native support stands today.

Verified on December 4, 2026 on Node.js 26.14.0 (LTS) with TZ=America/New_York, temporal-polyfill 1.0.9, and MDN compatibility data 8.1.12. Error examples log error.name because engines and polyfills word their messages differently.

Where Temporal works today

Temporal is a finished, Stage 4 proposal, with 2027 as its expected publication year. Most engines ship it now. Safari's support is still in beta.

RuntimeNative Temporal
Chrome and EdgeYes, from 144. Chrome 144 shipped in January 2026
FirefoxYes, from 139, with the withCalendar() gap described below
SafariNot in stable 27.2. Supported in the 27.3 beta, macOS and iOS
Node.jsYes, from 26.0.0
DenoYes, from 2.7
BunYes, from 1.4.0

Node 26, the current LTS line, exposes Temporal without a flag. Node 22 and 24 are still supported, but typeof Temporal is undefined there without a flag or polyfill (checked on 22.23.5 and 24.23.0). It's also undefined on Node 25 (25.2.1 and 25.9.0), which reached end of life on June 1, 2026, so move to 26 rather than polyfilling it long term.

Pick the type before writing code

Date uses one type for several different things. Temporal makes you choose:

  • Temporal.Instant: an exact moment, with no time zone. Use it for createdAt, logs and token expiry.
  • Temporal.PlainDate: a calendar date, with no time or zone. Use it for birthdays, due dates and invoice dates.
  • Temporal.ZonedDateTime: an exact moment in a named time zone. Use it for meetings and schedules.
  • Temporal.PlainDateTime: a wall clock reading with no zone. Use it for form input before the zone is known.
  • Temporal.Duration: an amount of time.

Get the current time

Temporal.Now replaces new Date() and Date.now(). Pick the method that returns the type you need:

console.log(Temporal.Now.instant().toString())
// 2026-12-03T21:48:15.000090088Z

console.log(Temporal.Now.plainDateISO().toString())
// 2026-12-03  (the system zone is America/New_York)

console.log(Temporal.Now.plainDateISO('Asia/Karachi').toString())
// 2026-12-04

console.log(Temporal.Now.zonedDateTimeISO('Asia/Karachi').toString())
// 2026-12-04T02:48:15.085656982+05:00[Asia/Karachi]

Your values will differ. Without an argument, plainDateISO and zonedDateTimeISO use the system time zone. On a server, pass the user's zone, because "today" depends on it. If you need a number for an existing API, use Temporal.Now.instant().epochMilliseconds.

Change a field without mutation

Temporal values are immutable. Where you'd call setDate(1) on a Date, call with and keep the new value:

const due = Temporal.PlainDate.from('2026-12-04')
const firstOfMonth = due.with({ day: 1 })

console.log(firstOfMonth.toString()) // 2026-12-01
console.log(due.toString()) // 2026-12-04

Store a calendar date without shifting it

new Date('2026-03-01') is midnight UTC. In New York that is still the evening before:

const date = new Date('2026-03-01')
console.log(date.getDate()) // 28

const due = Temporal.PlainDate.from('2026-03-01')
console.log(due.day) // 1
console.log(due.toString()) // 2026-03-01

PlainDate cannot shift, because it has no time zone to convert. It also refuses a string that names an exact UTC moment:

try {
  Temporal.PlainDate.from('2026-03-01T00:00:00Z')
} catch (error) {
  console.log(error.name) // RangeError
}

That error catches a real ambiguity. A UTC timestamp can fall on different calendar dates in different time zones, so pick a zone first:

const instant = Temporal.Instant.from('2026-03-01T02:00:00Z')
const local = instant.toZonedDateTimeISO('America/New_York').toPlainDate()
console.log(local.toString()) // 2026-02-28

Store an instant as an instant

Date reads a date-time without an offset in the machine's zone, so the same code can give different results on your laptop and your server. Instant rejects it:

console.log(new Date('2026-03-01T09:30:00').toISOString())
// 2026-03-01T14:30:00.000Z  (only because TZ=America/New_York)

try {
  Temporal.Instant.from('2026-03-01T09:30:00')
} catch (error) {
  console.log(error.name) // RangeError
}

const paidAt = Temporal.Instant.from('2026-03-01T09:30:00-05:00')
console.log(paidAt.toString()) // 2026-03-01T14:30:00Z
console.log(paidAt.toZonedDateTimeISO('Asia/Karachi').toString())
// 2026-03-01T19:30:00+05:00[Asia/Karachi]

An Instant does not keep the original offset. If the zone matters, store its name in a second column.

Add a day across a DST change

New York moved its clocks forward on March 8, 2026. "Tomorrow at the same time" and "24 hours later" are different operations:

const start = Temporal.ZonedDateTime.from('2026-03-07T09:00[America/New_York]')

console.log(start.add({ days: 1 }).toString())
// 2026-03-08T09:00:00-04:00[America/New_York]
console.log(start.add({ hours: 24 }).toString())
// 2026-03-08T10:00:00-04:00[America/New_York]
console.log(start.add({ days: 1 }).hoursInDay) // 23

Use days for reminders, bookings and recurring events. Use hours for timeouts and expiry.

When the clocks change, some local times are skipped and others happen twice. The default, compatible, shifts skipped times forward and picks the earlier of two repeated times. Pass disambiguation to override that:

const missing = '2026-03-08T02:30[America/New_York]'
console.log(Temporal.ZonedDateTime.from(missing).toString())
// 2026-03-08T03:30:00-04:00[America/New_York]

try {
  Temporal.ZonedDateTime.from(missing, { disambiguation: 'reject' })
} catch (error) {
  console.log(error.name) // RangeError
}

const twice = '2026-11-01T01:30[America/New_York]'
console.log(Temporal.ZonedDateTime.from(twice, { disambiguation: 'earlier' }).offset) // -04:00
console.log(Temporal.ZonedDateTime.from(twice, { disambiguation: 'later' }).offset) // -05:00

Use reject for user-entered times, so you can ask for a different time instead of shifting it silently.

Add a month at the end of the month

Date overflows into March. Temporal clamps to the last valid day:

const date = new Date(2026, 0, 31)
date.setMonth(1)
console.log(date.toDateString()) // Tue Mar 03 2026

const jan = Temporal.PlainDate.from('2026-01-31')
console.log(jan.add({ months: 1 }).toString()) // 2026-02-28
console.log(jan.add({ months: 2 }).toString()) // 2026-03-31
console.log(jan.add({ months: 1 }).add({ months: 1 }).toString()) // 2026-03-28

Look at the last two lines. For monthly billing, keep the original anchor date and add 1, 2, 3 months to it. If you add one month to the previous result, the date drifts to the 28th and stays there.

Measure differences and durations

until on two plain dates gives days by default. Ask for larger units when you want them:

const from = Temporal.PlainDate.from('2026-01-31')
const to = Temporal.PlainDate.from('2026-12-04')
console.log(from.until(to).days) // 307
console.log(from.until(to, { largestUnit: 'year' }).toString()) // P10M4D

const start = Temporal.Instant.from('2026-12-04T09:00:00Z')
const end = Temporal.Instant.from('2026-12-04T11:47:30Z')
const took = start.until(end, { largestUnit: 'hour' })
console.log(took.toString()) // PT2H47M30S
console.log(took.total('minute')) // 167.5
console.log(took.round({ smallestUnit: 'minute' }).toString()) // PT2H48M

Temporal doesn't normalize durations automatically (90 minutes stays PT90M), and calendar units such as months have no fixed length. Call round to balance units, and pass relativeTo when the answer depends on the calendar:

console.log(Temporal.Duration.from({ minutes: 90 }).toString()) // PT90M
console.log(Temporal.Duration.from({ minutes: 90 }).round({ largestUnit: 'hour' }).toString())
// PT1H30M

const month = Temporal.Duration.from({ months: 1 })
console.log(month.total({ unit: 'day', relativeTo: '2026-02-01' })) // 28

Without relativeTo, that last call throws a RangeError.

Parse strictly and format with a locale

Date accepts February 31 and rolls it forward. PlainDate rejects it:

console.log(new Date('2026-02-31').toISOString()) // 2026-03-03T00:00:00.000Z

for (const input of ['2026-02-31', '12/04/2026']) {
  try {
    Temporal.PlainDate.from(input)
  } catch (error) {
    console.log(input, error.name)
  }
}
// 2026-02-31 RangeError
// 12/04/2026 RangeError

There is one catch. Object input clamps by default, so pass overflow: 'reject' when you build a date from separate form fields:

const fields = { year: 2026, month: 2, day: 31 }
console.log(Temporal.PlainDate.from(fields).toString()) // 2026-02-28

try {
  Temporal.PlainDate.from(fields, { overflow: 'reject' })
} catch (error) {
  console.log(error.name) // RangeError
}

Months are 1 to 12 in Temporal, not 0 to 11.

Temporal does not parse or print custom patterns such as DD/MM/YYYY. For display, use toLocaleString:

const day = Temporal.PlainDate.from('2026-12-04')
console.log(day.toLocaleString('en-GB', { dateStyle: 'long' })) // 4 December 2026

const instant = Temporal.Instant.from('2026-12-04T09:00:00Z')
console.log(
  instant.toLocaleString('en-US', {
    timeZone: 'Asia/Karachi',
    dateStyle: 'medium',
    timeStyle: 'short',
  })
) // Dec 4, 2026, 2:00 PM

Intl.DateTimeFormat also formats plain dates and instants. It throws a TypeError for a ZonedDateTime, so call that object's own toLocaleString.

Compare and sort

The < and > operators throw on Temporal objects, and === compares object identity. Use equals and the static compare:

const a = Temporal.PlainDate.from('2026-01-31')
const b = Temporal.PlainDate.from('2026-12-04')

console.log(a.equals('2026-01-31')) // true
console.log(Temporal.PlainDate.compare(a, b)) // -1
console.log([b, a].sort(Temporal.PlainDate.compare).join(', '))
// 2026-01-31, 2026-12-04

const karachi = Temporal.ZonedDateTime.from('2026-12-04T09:00[Asia/Karachi]')
const utc = Temporal.ZonedDateTime.from('2026-12-04T04:00[UTC]')
console.log(Temporal.ZonedDateTime.compare(karachi, utc)) // 0
console.log(karachi.equals(utc)) // false
console.log(karachi.toInstant().equals(utc.toInstant())) // true

compare on zoned values orders by the exact moment. equals also checks the time zone and calendar.

Convert at API and database boundaries

You do not have to migrate everything at once. Convert on the way in and on the way out:

// In: a database driver that returns Date for a timestamp and a string for a date
const row = { created_at: new Date('2026-12-04T09:00:00.000Z'), due_on: '2026-12-31' }

const order = {
  createdAt: row.created_at.toTemporalInstant(),
  dueOn: Temporal.PlainDate.from(row.due_on),
}

// Out: JSON.stringify calls toJSON on each Temporal value
console.log(JSON.stringify(order))
// {"createdAt":"2026-12-04T09:00:00Z","dueOn":"2026-12-31"}

// Out: a library that still wants Date
console.log(new Date(order.createdAt.epochMilliseconds).toISOString())
// 2026-12-04T09:00:00.000Z

Four things to watch:

  • new Date(instant) throws a TypeError. Go through epochMilliseconds.
  • JSON.parse gives you strings. Call Temporal.Instant.from or Temporal.PlainDate.from on each field.
  • Date keeps milliseconds and Instant keeps nanoseconds, so a trip through Date drops the extra digits.
  • structuredClone throws a DataCloneError on Node 26. On Node 24 and 25 with temporal-polyfill or @js-temporal/polyfill, it returns an empty object instead. Send strings to workers.

If your driver turns a SQL DATE into a Date, configure it to return a string. Once it becomes a Date, the calendar date depends on a time zone again.

For a future meeting, store the wall time and zone name, then compute the instant. If a government changes the zone rules, the meeting stays at 9 AM local time:

const meeting = { local: '2026-12-04T09:00', timeZone: 'America/New_York' }
const zoned = Temporal.PlainDateTime.from(meeting.local).toZonedDateTime(meeting.timeZone)
console.log(zoned.toInstant().toString()) // 2026-12-04T14:00:00Z

Don't store the computed offset too. If the rules change, ZonedDateTime.from throws a RangeError by default for a string whose offset no longer matches its zone.

Should you ship it today

  • Server on Node 26, Deno 2.7+ or Bun 1.4+: yes, use the native global.
  • Server on Node 22 or 24: yes, with a polyfill until you upgrade. Both expose Temporal behind --harmony-temporal, but don't rely on an engine flag in production.
  • Public website: yes, with a polyfill. Stable Safari 27.2 has no Temporal, and older Safari versions will stay in use after 27.3 ships.
  • Non-Gregorian calendars: test first. MDN data marks withCalendar() as partial in Firefox for islamic, islamic-rgsa and islamic-umalqura.

There are three polyfills on the proposal's list:

PackageLatest version, publishedStatus on that list
temporal-polyfill1.0.9, 2026-11-29Stable
temporal-polyfill-lite1.0.2, 2026-11-15Release candidate
@js-temporal/polyfill0.5.1, 2025-03-31Alpha

The status for temporal-polyfill-lite looks out of date, since npm's latest tag is already 1.0.2.

Start with temporal-polyfill. Its package README lists 19.1 kB min+gzip, and it uses the native implementation when there is one:

npm install temporal-polyfill
// Load once, at your app entry point
import 'temporal-polyfill/global'

console.log(Temporal.PlainDate.from('2026-12-04').toString()) // 2026-12-04

The global import works on Node 22, 24, 25 and 26, and includes Date.prototype.toTemporalInstant. In a shared library, import the object instead so you don't change globals:

import { Temporal } from 'temporal-polyfill'

The default entry covers the ISO and Gregorian calendars. Use temporal-polyfill/full/global for the rest.

For types, TypeScript 7.0.2 includes Temporal in its esnext lib. Set "target": "esnext" in tsconfig.json, or include "esnext" in lib. A lib of es2024 and dom alone doesn't include it.

When to use this

Start using Temporal for new calendar-date and scheduling code. Leave working timestamp code alone until you have another reason to change it, and convert at API or database boundaries. When a date field can be missing, the nullish coalescing operator is a clean way to supply a default before parsing.

References