Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Formatting Times With toLocaleString()

Python’s format pattern selects the parts of a date and time. Its weekday and month names use the process’s time locale. In JavaScript, name the parts in an options object and supply a locale explicitly. For the clinic’s British English output, format the patient’s ZonedDateTime like this:

Language: JavaScript
const TIME_FORMAT = {
  weekday: "long",
  day: "2-digit",
  month: "long",
  hour: "2-digit",
  minute: "2-digit",
};
const start = Temporal.Instant.from(
  "2026-11-01T15:00:00Z",
).toZonedDateTimeISO("America/New_York");
console.log(start.toLocaleString("en-GB", TIME_FORMAT));

The result keeps Dana’s New York time:

Language: Program Output
Sunday 01 November at 10:00

Intl.DateTimeFormat().format() rejects a ZonedDateTime even if the zones match. Giving that formatter an Instant or Date instead uses the formatter’s zone, normally the machine’s. The ZonedDateTime’s own toLocaleString() method uses its zone. Always name the locale; an omitted locale uses the environment’s default.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 You’ll restore the clinic’s readable appointment times. Dana’s Sunday call is at ten in New York. The date and reminder calculations are already finished.

00:08 Now the message needs the right words around that time. In Python, the existing pattern chooses a weekday, day, month, hour and minute. Its output is Sunday first November at ten.

00:23 Those English names come from this Python process’s time locale. In JavaScript, an options object names those parts.

00:36 Two-digit fields keep the leading zero. A locale selects the language and conventions; British English matches this clinic message.

00:48 For the JavaScript port, a reusable formatter looks familiar from formatting numbers. DateTimeFormat takes the locale and the date options. The value called start already carries Dana’s New York zone.

01:03 The last line passes that value to the formatter. What does this call do? Your turn: pause the video and predict the result.

01:17 It throws a TypeError. The message identifies the ZonedDateTime argument. The pattern and locale are valid; this format method won’t accept that kind of value.

01:33 A date formatter has its own time zone, normally the machine’s. It rejects a ZonedDateTime even if their zones match. That value already carries the zone the patient’s message needs.

01:47 Call toLocaleString() on the zoned value itself. Keep the locale and the options. JavaScript now prints Sunday first November at ten, matching Python.

01:59 Watch out: giving that formatter an Instant or Date uses its own zone, showing fifteen hundred on a London machine and ten on a New York machine.

02:10 The zoned value’s own method keeps Dana’s time on either machine.

02:18 Keep British English in the call and the parts in one named constant. That makes the clinic’s display choice visible wherever the pattern is reused. Watch out: omitting the locale uses the environment’s default, so this US-English setup prints eleven slash one slash twenty twenty-six, ten A M, EST.

02:40 Your machine may use different words and punctuation. With component options, a short time-zone name adds EST in American English or GMT-5 in British English.

02:54 The locale controls that label too; the appointment still stays at ten in New York.

03:03 Patients may want their usual date format. Python’s pattern uses the process’s time locale; choosing a separate locale for every reader needs a locale change or another library.

03:15 In JavaScript, dateStyle and timeStyle offer full, long, medium or short forms. Choose a full date and a short time instead of component options.

03:27 For Dana, American English gives Sunday, November first, twenty twenty-six, at ten A M. Fiona’s British English keeps the twenty-four-hour clock. Jonas’s German changes the words and date punctuation.

03:44 Each value still carries that patient’s time zone.

03:50 For the clinic’s report, keep the original part options and British English. The header uses London’s time; each patient’s line uses their time. The eight lines now match the Python output.

04:04 The rule is: format the zoned value with its own method, a named locale and named display options. These strings are for people. Compare the original date and time values, not the formatted text.

04:19 Next up: Remind Each Patient in Their Language. You’ll give Fiona, Dana and Jonas their requested appointment displays, each in their own time.

Become a Member to join the conversation.