Sub topic 10
Displaying an Instant in Any Time Zone
This chapter assumes you know that a Date stores one instant as epoch milliseconds, and that you have created a formatter with Intl.DateTimeFormat. Here you will show that one instant as wall-clock time in any zone, not only the zone the runtime runs in.
The Time Zone Is a Display Setting
A Date has no time zone. The timeZone option tells a formatter which zone's rules to use when it turns the stored milliseconds into a year, month, day, hour, and minute. If you leave the option out, the formatter uses the runtime's default zone. That default is usually the user's operating system setting.
const instant = new Date("2024-07-04T16:30:00Z");
for (const timeZone of ["UTC", "Asia/Tokyo", "America/Los_Angeles"]) {
const text = instant.toLocaleString("en-GB", { timeZone, timeZoneName: "short" });
console.log(timeZone, text);
}
// e.g. UTC 04/07/2024, 16:30:00 UTC
// Asia/Tokyo 05/07/2024, 01:30:00 GMT+9
// America/Los_Angeles 04/07/2024, 09:30:00 PDTIn Tokyo the calendar date is different. Any "which day is it" logic has to use the zone you display, not the runtime's zone.
IANA Names Versus Fixed Offsets
IANA time zone names such as Europe/Paris come from the IANA tz database. Each name holds that region's full history of offsets and daylight saving rules. A fixed offset such as +02:00 is right for only part of the year in Paris.
| Value | Example | Follows DST? | Use it for |
|---|---|---|---|
| IANA region name | `America/New_York` | Yes | Showing times to people in a place |
| `UTC` | `UTC` | No offset changes | Logs, servers, unambiguous output |
| Offset string | `+05:30` | No | Data that was recorded with a fixed offset |
Validating and Discovering Zones
Time zone names often come from user settings or an API. A formatter throws a RangeError if it gets an unknown zone, so validate the name before you use it:
function isValidTimeZone(timeZone) {
try {
new Intl.DateTimeFormat("en-US", { timeZone });
return true;
} catch (err) {
if (err instanceof RangeError) return false;
throw err;
}
}
console.log(isValidTimeZone("Asia/Kolkata")); // true
console.log(isValidTimeZone("Mars/Olympus")); // false
// Modern engines can list the zones they know:
if (typeof Intl.supportedValuesOf === "function") {
const zones = Intl.supportedValuesOf("timeZone");
console.log(zones.length, zones.includes("Europe/Paris"));
}To show the offset itself, use timeZoneName: "longOffset":
const fmt = new Intl.DateTimeFormat("en-US", {
timeZone: "Europe/Paris",
timeStyle: "short",
timeZoneName: "longOffset",
});
console.log(fmt.format(new Date("2024-01-15T12:00:00Z"))); // e.g. "1:00 PM GMT+01:00"
console.log(fmt.format(new Date("2024-07-15T12:00:00Z"))); // e.g. "2:00 PM GMT+02:00"The offset changes between January and July because the IANA rules for Paris include daylight saving time.
Common Errors
- Shifting the milliseconds to "convert" a zone, as in
new Date(d.getTime() + 9 * 3600000). This creates a different instant. PasstimeZoneto the formatter instead. - Storing only an offset when you need the place. An offset loses the future DST rules, so store the IANA name.
- Assuming the server's zone is the user's zone. Pass the user's zone explicitly when rendering on a server.
Exercise
An event is stored as "2024-12-31T20:00:00Z". A user in "Asia/Tokyo" sees it labeled with the year 2024 because the code calls getFullYear() on a server running in UTC. What is wrong, and how do you get the year the user sees?
Show answer
getFullYear() uses the runtime's zone, which is UTC here. In Tokyo (UTC+9) that instant is 2025-01-01 at 05:00. Format with the user's zone instead: new Intl.DateTimeFormat("en-US", { year: "numeric", timeZone: "Asia/Tokyo" }).format(new Date("2024-12-31T20:00:00Z")) returns "2025".
Exercise
Why does a formatter with timeZone "America/New_York" show different offsets for dates in January and July, while timeZone "-05:00" does not?
Show answer
The IANA name carries New York's daylight saving rules, so it applies −05:00 in winter and −04:00 in summer. A fixed offset string has no rules and always applies −05:00.