Sub topic 16
Formatting Dates and Times for Display
This chapter assumes you can create Temporal values with from() and know the difference between the Plain types and ZonedDateTime. It also helps to have used Intl.DateTimeFormat with legacy Date. The examples assume Temporal is already available, either natively or through the setup from the runtime support chapter.
Temporal gives you three ways to turn a value into text, and each one fits a different job:
| Method | Output | Use it for |
|---|---|---|
| `toString()` | ISO 8601 / RFC 9557 string | Logs, APIs, storage, round-tripping |
| `toLocaleString()` | Localized human text | One-off display |
| `Intl.DateTimeFormat` | Localized human text | Repeated display, ranges, parts |
Machine-readable output with toString
toString() produces a string that from() can parse back into the same value. Options trim the output without changing the format.
const zdt = Temporal.ZonedDateTime.from('2024-03-15T09:05:30.123[Europe/Paris]');
console.log(zdt.toString());
// '2024-03-15T09:05:30.123+01:00[Europe/Paris]'
console.log(zdt.toString({ smallestUnit: 'minute', timeZoneName: 'never' }));
// '2024-03-15T09:05+01:00'
console.log(zdt.toString({ fractionalSecondDigits: 0 }));
// '2024-03-15T09:05:30+01:00[Europe/Paris]'smallestUnit and fractionalSecondDigits truncate by default, and you can pass roundingMode to change that. Don't build display strings by slicing toString() output. Use the locale-aware methods for anything a person reads.
Human-readable output with toLocaleString
toLocaleString() takes the same locale and options arguments as Date.prototype.toLocaleString().
const date = Temporal.PlainDate.from('2024-03-15');
console.log(date.toLocaleString('en-US', { dateStyle: 'long' })); // 'March 15, 2024'
const zdt = Temporal.ZonedDateTime.from('2024-03-15T09:05[Europe/Paris]');
console.log(zdt.toLocaleString('en-GB', { dateStyle: 'medium', timeStyle: 'short' }));
// '15 Mar 2024, 09:05'A ZonedDateTime is always formatted in its own time zone. To show it in a different zone, call withTimeZone() first. Passing a different timeZone option throws a RangeError.
Reusing a formatter with Intl.DateTimeFormat
Creating a formatter costs more than calling it. When you render a list of values, create one Intl.DateTimeFormat and reuse it. Its format(), formatToParts(), and formatRange() methods all accept Temporal objects.
const fmt = new Intl.DateTimeFormat('de-DE', { dateStyle: 'full' });
const dates = ['2024-03-15', '2024-12-24'].map((s) => Temporal.PlainDate.from(s));
console.log(dates.map((d) => fmt.format(d)));
// ['Freitag, 15. März 2024', 'Dienstag, 24. Dezember 2024']
const range = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' });
console.log(range.formatRange(dates[0], dates[1])); // 'Mar 15 – Dec 24, 2024'Common errors
- Asking for fields the type lacks. A
PlainDatehas no time, so{ timeStyle: 'short' }throws aTypeError. Plain types have no time zone either, sotimeZoneNamethrows on them too. - Mixing types in
formatRange. Both arguments must be the same Temporal type. - Comparing localized strings in tests. ICU updates can change spacing. For example, en-US times now use a narrow no-break space before "AM". Assert on
toString()orformatToParts()instead. - Expecting
Instantto pick a zone.Instant.toLocaleString()uses the runtime's default time zone unless you passtimeZone. Convert explicitly when the zone matters.
Exercise
You have a list of 500 ZonedDateTime values in America/New_York and must display each one for Tokyo users as a medium date and short time in Japanese. Which approach do you use, and why does passing { timeZone: 'Asia/Tokyo' } to toLocaleString fail?
Show answer
Convert each value with withTimeZone('Asia/Tokyo'). Then format all of them with one shared Intl.DateTimeFormat('ja-JP', { dateStyle: 'medium', timeStyle: 'short' }), which is cheaper than creating 500 formatters. Passing a timeZone that differs from the ZonedDateTime's own zone throws a RangeError, because a ZonedDateTime is always formatted in its own time zone.
Exercise
Why does Temporal.PlainTime.from('14:30').toLocaleString('en-US', { dateStyle: 'short' }) throw?
Show answer
A PlainTime has no date fields, so a date-style option asks for data the type doesn't have, and the call throws a TypeError. Use timeStyle or hour/minute options instead.