Sub topic 3
The toLocale* Shortcuts and When to Use Them
This chapter assumes you know how a Date stores an instant and how to build an Intl.DateTimeFormat with a locale, options, and a timeZone.
The toLocale* Shortcuts
toLocaleString, toLocaleDateString, and toLocaleTimeString accept the same locale and options arguments as Intl.DateTimeFormat. They are handy for a single value. Each call can build a new formatter internally, so avoid them inside loops.
const d = new Date(Date.UTC(2024, 6, 4, 16, 30));
console.log(d.toLocaleDateString("en-US", { timeZone: "UTC" })); // "7/4/2024"
console.log(d.toLocaleTimeString("en-GB", { timeZone: "UTC" })); // "16:30:00"Custom Layouts and Date Spans
formatToParts returns typed pieces such as day, month, and literal. You can rearrange or style these pieces without parsing a formatted string. formatRange formats two dates as one span and merges any fields the two dates share.
const fmt = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeZone: "UTC" });
const a = new Date(Date.UTC(2024, 0, 10));
const b = new Date(Date.UTC(2024, 0, 12));
console.log(fmt.formatToParts(a).map((p) => p.type)); // ["month", "literal", "day", "literal", "year"]
console.log(fmt.formatRange(a, b)); // e.g. "Jan 10 – 12, 2024"Relative Times, Plurals, and Numbers
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
console.log(rtf.format(-1, "day")); // "yesterday"
console.log(rtf.format(-3, "day")); // "3 days ago"
const ordinal = new Intl.PluralRules("en-US", { type: "ordinal" });
const suffix = { one: "st", two: "nd", few: "rd", other: "th" };
console.log([1, 2, 3, 11].map((n) => n + suffix[ordinal.select(n)])); // ["1st", "2nd", "3rd", "11th"]
const eur = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" });
console.log(eur.format(1234.5)); // e.g. "1.234,50 €"RelativeTimeFormat only formats a number you pass in. You pick the unit yourself. PluralRules returns a category name such as "one" or "few", and you map that category to your own words.
Reusing Formatters
Constructing an Intl object costs far more than calling format on it. Create each formatter once, at module level, and reuse it.
Looking Ahead: Temporal
Temporal is the newer date-time API. It provides separate immutable types such as Temporal.PlainDate, Temporal.Instant, and Temporal.ZonedDateTime, and its arithmetic follows time zone rules. Support differs between runtimes, so check for it before you use it:
const hasTemporal = typeof globalThis.Temporal !== "undefined";
console.log(hasTemporal ? "Temporal available" : "Use Date + Intl, or a polyfill");Exercise
You render 5,000 table rows, each calling date.toLocaleDateString("en-US", { dateStyle: "medium" }). What change speeds this up, and why?
Show answer
Create one Intl.DateTimeFormat("en-US", { dateStyle: "medium" }) outside the loop and call its format method for each row. Building a formatter is the expensive step, and toLocaleDateString may build a new one on every call.
Exercise
How would you print "in 2 weeks" and "last week" with one formatter?
Show answer
Use new Intl.RelativeTimeFormat("en", { numeric: "auto" }), then call format(2, "week") and format(-1, "week").