Sub topic 18
Converting Between Temporal Types
This chapter assumes you know the Temporal types from earlier chapters: Instant, ZonedDateTime, and the Plain types. You should also be able to create values with from(). The examples use the Temporal global, either native or loaded through the polyfill fallback shown in the runtime support chapter.
Real programs move between types all the time. A server stores an Instant. The UI shows a ZonedDateTime in the user's zone. A date picker returns a PlainDate. Each conversion either drops information or asks you to supply it. Once you know which way a conversion goes, the method names follow.
The Rule: Drop Freely, Supply Explicitly
The types form a ladder by how much information each one holds:
| Type | Holds | Missing |
|---|---|---|
| `ZonedDateTime` | Exact instant, time zone, calendar | Nothing |
| `Instant` | Exact instant | Time zone, calendar |
| `PlainDateTime` | Date and wall-clock time | Time zone (so no exact instant) |
| `PlainDate` | Date | Time, time zone |
| `PlainTime` | Wall-clock time | Date, time zone |
| `PlainYearMonth` | Year and month | Day, time, time zone |
| `PlainMonthDay` | Month and day | Year, time, time zone |
Moving toward less information takes no arguments. Methods such as toPlainDate() just discard fields. Moving toward more information always takes an argument: a time zone, a time, a day, or a year. Temporal never invents missing data silently. The one exception is a default time of midnight, covered below.
Between Exact Time and Wall-Clock Time
An Instant becomes a ZonedDateTime once you name a time zone. Going back with toInstant() loses nothing you can't recover, because the ZonedDateTime still holds the exact instant.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const instant = Temporal.Instant.from('2024-03-15T12:00:00Z');
const tokyo = instant.toZonedDateTimeISO('Asia/Tokyo');
console.log(tokyo.toString()); // '2024-03-15T21:00:00+09:00[Asia/Tokyo]'
console.log(tokyo.toInstant().equals(instant)); // true
// Same instant, different wall clock
const nyc = tokyo.withTimeZone('America/New_York');
console.log(nyc.toString()); // '2024-03-15T08:00:00-04:00[America/New_York]'withTimeZone() is a conversion that keeps the type. It keeps the instant and changes the wall-clock reading. The ISO in toZonedDateTimeISO means the result uses the ISO 8601 calendar.
Extracting Plain Parts
You can drop the zone from a ZonedDateTime or PlainDateTime to get Plain values. Plain values are what you compare with "the user's birthday" or "the 9:00 opening time", which are facts that don't depend on a time zone.
const zdt = Temporal.ZonedDateTime.from('2024-03-15T21:45[Asia/Tokyo]');
const dateTime = zdt.toPlainDateTime();
console.log(dateTime.toString()); // '2024-03-15T21:45:00'
console.log(zdt.toPlainDate().toString()); // '2024-03-15'
console.log(zdt.toPlainTime().toString()); // '21:45:00'
console.log(dateTime.toPlainDate().toString()); // '2024-03-15'
// Year-month and month-day come from a PlainDate
const date = zdt.toPlainDate();
console.log(date.toPlainYearMonth().toString()); // '2024-03'
console.log(date.toPlainMonthDay().toString()); // '03-15'Supplying What Is Missing
Adding a time to a date
PlainDate.prototype.toPlainDateTime() takes an optional time. If you leave it out, the time is midnight. To change only the time of an existing PlainDateTime, use withPlainTime().
const date = Temporal.PlainDate.from('2024-03-15');
console.log(date.toPlainDateTime().toString()); // '2024-03-15T00:00:00'
console.log(date.toPlainDateTime('09:30').toString()); // '2024-03-15T09:30:00'
const dt = Temporal.PlainDateTime.from('2024-03-15T09:30');
console.log(dt.withPlainTime('17:00').toString()); // '2024-03-15T17:00:00'Adding a time zone
PlainDate.prototype.toZonedDateTime() accepts either a time zone string or an object with timeZone and an optional plainTime. With no time, you get the first valid moment of that day in that zone. That is usually midnight, but not always (see below).
const date = Temporal.PlainDate.from('2024-03-15');
console.log(date.toZonedDateTime('America/New_York').toString());
// '2024-03-15T00:00:00-04:00[America/New_York]'
console.log(
date.toZonedDateTime({ timeZone: 'Europe/Paris', plainTime: '14:00' }).toString()
);
// '2024-03-15T14:00:00+01:00[Europe/Paris]'Wall-clock times that don't exist
PlainDateTime.prototype.toZonedDateTime() converts a wall-clock reading into an exact moment. That reading might be skipped or repeated by a daylight saving change. The disambiguation option controls the outcome. The default, 'compatible', moves a skipped time forward. 'reject' throws instead.
// 02:30 never happened in New York on 2024-03-10
const skipped = Temporal.PlainDateTime.from('2024-03-10T02:30');
console.log(skipped.toZonedDateTime('America/New_York').toString());
// '2024-03-10T03:30:00-04:00[America/New_York]'
try {
skipped.toZonedDateTime('America/New_York', { disambiguation: 'reject' });
} catch (e) {
console.log(e instanceof RangeError); // true
}The daylight saving chapter covers the other options. Here, the point is that this conversion direction is the one that can be ambiguous.
Completing partial dates
PlainYearMonth needs a day, and PlainMonthDay needs a year. Both conversions constrain values that don't exist in the target year.
const ym = Temporal.PlainYearMonth.from('2024-02');
console.log(ym.toPlainDate({ day: 1 }).toString()); // '2024-02-01'
const leapDay = Temporal.PlainMonthDay.from('02-29');
console.log(leapDay.toPlainDate({ year: 2024 }).toString()); // '2024-02-29'
console.log(leapDay.toPlainDate({ year: 2023 }).toString()); // '2023-02-28'If you treat a birthday on February 29 this way, it falls on February 28 in non-leap years. Decide whether that matches your business rule.
Start of Day Is Not Always Midnight
Some zones skip midnight itself on their daylight saving day. To get the true first moment of a day, use ZonedDateTime.prototype.startOfDay(). Don't build the value with withPlainTime('00:00').
// Santiago, Chile springs forward at midnight
const day = Temporal.ZonedDateTime.from('2024-09-08T12:00[America/Santiago]');
console.log(day.startOfDay().toString());
// '2024-09-08T01:00:00-03:00[America/Santiago]'Common Errors
- Looking for
PlainDateTime.prototype.toInstant(). It doesn't exist, because a wall-clock time has no exact moment without a zone. Use.toZonedDateTime(tz).toInstant(). - Calling
instant.toZonedDateTimeISO()with no argument. The time zone is required, and leaving it out throws aTypeError. For the system zone, passTemporal.Now.timeZoneId(). - Round trips across a gap. Converting
ZonedDateTimetoPlainDateTimeand back usually gives the same value. It can shift when the wall-clock time is repeated, because the offset was dropped. Keep theZonedDateTimeif you need to recover the exact moment. - Dropping the zone too early. If you convert to
PlainDatebefore doing arithmetic, you can't account for daylight saving changes later. Do zone-sensitive work onZonedDateTime, then extract Plain parts for display or comparison.
Exercise
A server sends the instant '2024-11-03T05:30:00Z'. Write code that prints the calendar date and the wall-clock time a user in America/Los_Angeles sees for it.
Show answer
const zdt = Temporal.Instant.from('2024-11-03T05:30:00Z').toZonedDateTimeISO('America/Los_Angeles'); console.log(zdt.toPlainDate().toString(), zdt.toPlainTime().toString()); prints '2024-11-02 22:30:00'. Los Angeles was still on PDT (UTC-7) at that instant, so 05:30Z is 22:30 on the previous day.
Exercise
You have a PlainDate for a meeting and a PlainTime of 09:00. How do you get the exact Instant for that meeting in Europe/Berlin, and why can't you go straight from PlainDateTime to Instant?
Show answer
date.toZonedDateTime({ timeZone: 'Europe/Berlin', plainTime: time }).toInstant(). A PlainDateTime has no time zone, so it can match many exact moments. Temporal makes you supply the zone through ZonedDateTime before it will produce an Instant.
Exercise
What does Temporal.PlainMonthDay.from('02-29').toPlainDate({ year: 2025 }).toString() return, and why?
Show answer
'2025-02-28'. 2025 is not a leap year, and PlainMonthDay.toPlainDate constrains an invalid day to the last valid day of the month.