Sub topic 17
Working Alongside Legacy Date Code
Most codebases cannot switch to Temporal all at once. Libraries, database drivers, and older modules still produce and expect Date objects. This chapter shows how to move values across that boundary without silently losing information. It assumes you already know Temporal.Instant, ZonedDateTime, and the plain types from earlier chapters, and that you have used Date before.
The bridge: epoch milliseconds
A Date is a wrapper around one number: milliseconds since 1970-01-01T00:00Z. That is exact time, so its natural counterpart in Temporal is Temporal.Instant. Both APIs can read and write epoch milliseconds exactly, so that number is the safe crossing point.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
// Legacy Date to Temporal
const legacy = new Date('2024-03-15T12:00:00Z');
const instant = Temporal.Instant.fromEpochMilliseconds(legacy.getTime());
// Add the missing context: a time zone
const local = instant.toZonedDateTimeISO('America/New_York');
console.log(local.toString()); // '2024-03-15T08:00:00-04:00[America/New_York]'
// Temporal back to legacy Date
const roundTrip = new Date(instant.epochMilliseconds);
console.log(roundTrip.toISOString()); // '2024-03-15T12:00:00.000Z'The specification also adds Date.prototype.toTemporalInstant(). Native implementations provide it. The polyfill exports it as a separate toTemporalInstant function instead of patching Date.prototype. fromEpochMilliseconds works in both environments.
Common errors
Converting a Date straight to a PlainDate
A Date has no time zone of its own. Its getDate() and similar methods use the host machine's zone, so the same code can give different calendar days on a server and on a laptop. Go through Instant and name the zone you want:
const d = new Date('2024-03-15T20:00:00Z');
const day = Temporal.Instant.fromEpochMilliseconds(d.getTime())
.toZonedDateTimeISO('UTC')
.toPlainDate();
console.log(day.toString()); // '2024-03-15'Losing precision on the way back
Temporal stores nanoseconds and Date stores milliseconds. epochMilliseconds rounds down, so anything below a millisecond is dropped:
const precise = Temporal.Instant.from('2024-03-15T12:00:00.123456789Z');
console.log(precise.epochMilliseconds); // 1710504000123Passing an invalid Date
new Date('garbage').getTime() returns NaN, and fromEpochMilliseconds(NaN) throws a RangeError. With Date the bad value would have spread quietly through your code. Here you get an exception at the boundary, so catch it there.
| Direction | Method | Watch out for |
|---|---|---|
| Date → Instant | `Instant.fromEpochMilliseconds(d.getTime())` | Invalid Date throws `RangeError` |
| Instant → Date | `new Date(i.epochMilliseconds)` | Sub-millisecond digits are dropped |
| Date → PlainDate | via Instant and `toZonedDateTimeISO(tz)` | Always name the time zone |
Exercise
A legacy API returns new Date('2024-03-15T20:00:00Z'). Write an expression that gives the calendar date in Asia/Tokyo as a Temporal.PlainDate, and state its string value.
Show answer
Temporal.Instant.fromEpochMilliseconds(d.getTime()).toZonedDateTimeISO('Asia/Tokyo').toPlainDate() gives '2024-03-16', because 20:00 UTC is 05:00 the next day in Tokyo (UTC+9).
Exercise
Why does converting Temporal.Instant.from('2024-03-15T12:00:00.999999Z') to a Date and back not return an equal Instant?
Show answer
Date keeps only milliseconds. epochMilliseconds rounds down to ...12:00:00.999Z, so the 999 microsecond-and-below digits (0.999999 becomes 0.999) are lost and the restored Instant is 999 nanoseconds earlier.