Sub topic 7
Exact Moments with Temporal.Instant
This chapter assumes you know modern JavaScript syntax, can run ES modules in Node.js or a browser with Temporal available (natively or through the polyfill), and have used the legacy Date object.
A Temporal.Instant is a single exact point on the timeline. It has no time zone, no calendar, and no year, month, or day fields. It stores one thing: the number of nanoseconds since the Unix epoch (1970-01-01T00:00:00Z). Use it for event timestamps, log entries, and any value where "when exactly did this happen?" matters more than "what did the clock on the wall say?"
Creating an Instant
An Instant can be created from an ISO 8601 string that includes a UTC offset or Z. If the string has no offset, the moment it describes is unknown, so parsing throws.
const a = Temporal.Instant.from('2024-03-15T12:00:00Z');
const b = Temporal.Instant.from('2024-03-15T14:00:00+02:00');
console.log(a.equals(b)); // true: the same moment
console.log(b.toString()); // '2024-03-15T12:00:00Z' (normalized to UTC)
try {
Temporal.Instant.from('2024-03-15T12:00:00'); // no offset
} catch (e) {
console.log(e instanceof RangeError); // true
}The offset only locates the moment and is not stored. After parsing, b contains no information that it came from a +02:00 string.
Epoch Values
You can also build an Instant from epoch numbers and read them back. Milliseconds are plain numbers. Nanoseconds are BigInt values, because a regular number cannot hold that many digits exactly.
const i = Temporal.Instant.fromEpochMilliseconds(1710504000000);
console.log(i.toString()); // '2024-03-15T12:00:00Z'
console.log(i.epochMilliseconds); // 1710504000000
console.log(i.epochNanoseconds); // 1710504000000000000n
const precise = Temporal.Instant.fromEpochNanoseconds(1710504000123456789n);
console.log(precise.toString()); // '2024-03-15T12:00:00.123456789Z'Arithmetic Uses Exact Units Only
An Instant has no time zone, so it cannot know how long a "day" is. A day can last 23 or 25 hours around a daylight saving change. For this reason add and subtract accept hours and smaller units only.
const start = Temporal.Instant.from('2024-03-15T12:00:00Z');
console.log(start.add({ hours: 36 }).toString()); // '2024-03-17T00:00:00Z'
try {
start.add({ days: 1 });
} catch (e) {
console.log(e instanceof RangeError); // true
}Displaying an Instant in a Time Zone
toString prints UTC by default. Its timeZone option prints the same moment with a local offset without changing the value.
const t = Temporal.Instant.from('2024-03-15T12:00:00Z');
console.log(t.toString({ timeZone: 'Asia/Tokyo' })); // '2024-03-15T21:00:00+09:00'If you need local date fields, such as the day of the week in Tokyo, you need a ZonedDateTime. That type is covered in a later chapter.
Exercise
A server log contains '2024-06-01T08:30:00-05:00'. Write code that parses it into an Instant and prints the UTC string and the epoch milliseconds. Then explain why instant.add({ days: 2 }) fails and give a working alternative for exactly 48 hours later.
Show answer
const i = Temporal.Instant.from('2024-06-01T08:30:00-05:00'); console.log(i.toString()); // '2024-06-01T13:30:00Z' and console.log(i.epochMilliseconds); // 1717248600000. The call add({ days: 2 }) throws a RangeError because an Instant has no time zone, so the length of a day is undefined. Use i.add({ hours: 48 }), which gives '2024-06-03T13:30:00Z'.