Time Zones with Intl

A date is one moment in time; what the clock on the wall says depends on where you are. Intl.DateTimeFormat turns a moment into text for a language and a time zone. Time zones have names like "Africa/Accra", "America/New_York" and "Asia/Tokyo"; leave timeZone out and it uses the device's own zone.

Ask for hour, minute and second as "2-digit", and set hourCycle: "h23" so the hours run 00 to 23 rather than adding AM and PM.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Add two cities your family or friends live in. Time zone names are written Continent/City, such as Europe/London or America/Chicago.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

The same formatter writes dates in words: { weekday: "long", month: "long", day: "numeric", year: "numeric" } gives Monday, October 12, 2026. And formatToParts hands back the result in labeled pieces; the piece with type "timeZoneName" is the zone's short name, such as EST. Some zones have no letters in English and come back as GMT+9.

EXAMPLE
loading editor…

◈ Graded Build

Write tz(), which returns undefined when the menu says local and the menu's value otherwise. Then write fmtTime(date), which returns the date as HH:MM:SS in the chosen zone using Intl.DateTimeFormat, with hours from 00 to 23.

Checked by using your page: clicking, typing and waiting, then reading what changed. 4 visible and 1 hidden checks.

loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Running Code Later, and Again →
← Back