Sub topic 8
Time Zones and IANA Identifiers
This chapter assumes you know how to load Temporal (natively or through the polyfill) and understand Temporal.Instant as an exact moment in time. It explains what a time zone is in Temporal and how to name one correctly.
What a Time Zone Is in Temporal
A time zone is a set of rules that maps exact instants to local wall-clock times. The rules hold more than a single offset from UTC. They also record when that offset changes, both for daylight saving time and for historical changes made by governments.
Temporal identifies time zones with string identifiers from the IANA Time Zone Database, which is the same database operating systems and browsers use. An IANA identifier usually has the form Area/Location, such as America/New_York, Europe/Paris, or Asia/Kolkata. The special identifier UTC is also valid.
Applying Identifiers to an Instant
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const instant = Temporal.Instant.from('2024-03-15T12:00:00Z');
for (const id of ['Asia/Tokyo', 'America/New_York', 'Asia/Kolkata']) {
const zdt = instant.toZonedDateTimeISO(id);
console.log(id, zdt.toPlainTime().toString(), zdt.offset);
}
// Asia/Tokyo 21:00:00 +09:00
// America/New_York 08:00:00 -04:00
// Asia/Kolkata 17:30:00 +05:30
console.log(Temporal.Now.timeZoneId()); // the system zone, e.g. 'Europe/London'New York reports -04:00 instead of its standard -05:00 because daylight saving time began on 10 March 2024. The database supplies that rule, so you never hard-code it.
IANA Identifiers vs Fixed Offsets
Temporal also accepts offset strings such as -05:00 as time zone identifiers. An offset string is fixed and never changes for daylight saving time.
const july = Temporal.Instant.from('2024-07-01T12:00:00Z');
console.log(july.toZonedDateTimeISO('-05:00').toString());
// '2024-07-01T07:00:00-05:00[-05:00]'
console.log(july.toZonedDateTimeISO('America/New_York').toString());
// '2024-07-01T08:00:00-04:00[America/New_York]'| Identifier kind | Example | Follows DST and history | Typical use |
|---|---|---|---|
| IANA name | `Europe/Paris` | Yes | User locations, scheduling |
| UTC | `UTC` | No changes needed | Logs, server timestamps |
| Fixed offset | `+05:30` | No | Data that only records an offset |
Common Errors
- Storing an offset when you mean a place. An offset like
-05:00describes one moment. Future dates in that location may need a different offset, so store the IANA name for anything a user schedules. - Abbreviations. Strings such as
ESTorPSTare ambiguous and are not reliable identifiers. UseAmerica/New_YorkorAmerica/Los_Angeles. - Invalid names. An unknown identifier throws a
RangeError, so validate user input at the boundary:
try {
Temporal.Now.zonedDateTimeISO('Mars/Olympus_Mons');
} catch (e) {
console.log(e instanceof RangeError); // true
}Exercise
An app stores a user's meeting zone as "-04:00" because that was the New York offset when the meeting was created in June. What goes wrong for a meeting in December, and what should the app store instead?
Show answer
In December, New York is on standard time at -05:00, so a fixed -04:00 offset shows the meeting one hour off from the user's local clock. The app should store the IANA identifier "America/New_York" so Temporal applies the correct offset for each date.