Sub topic 11
The toLocale* Shortcuts and When to Use Them
This chapter assumes you know how Intl.DateTimeFormat takes a locale and an options object, and that the timeZone option controls which zone an instant is displayed in. The toLocale* methods on Date use the same machinery with less typing.
Three Methods, One Engine
Every Date has three locale-aware methods. Each one accepts the same locales and options arguments as Intl.DateTimeFormat:
| Method | Default components when you pass none |
|---|---|
| `toLocaleString()` | year, month, day, hour, minute, second |
| `toLocaleDateString()` | year, month, day |
| `toLocaleTimeString()` | hour, minute, second |
The specification defines these methods in terms of Intl.DateTimeFormat. They differ only in which components they fill in by default.
const d = new Date(Date.UTC(2024, 6, 4, 16, 30)); // 2024-07-04T16:30:00Z
console.log(d.toLocaleString("en-US", { timeZone: "UTC" })); // e.g. "7/4/2024, 4:30:00 PM"
console.log(d.toLocaleDateString("en-US", { timeZone: "UTC" })); // "7/4/2024"
console.log(d.toLocaleTimeString("en-US", { timeZone: "UTC" })); // e.g. "4:30:00 PM"
console.log(d.toLocaleDateString("de-DE", { timeZone: "UTC" })); // "4.7.2024"
// Once you name any component yourself, the defaults are not added
console.log(d.toLocaleDateString("en-US", { month: "long", timeZone: "UTC" })); // "July"Newer engines may put a narrow no-break space before "PM", so don't compare these strings with hard-coded literals in tests.
You can check that a shortcut and an explicit formatter give the same output:
const d = new Date(Date.UTC(2024, 6, 4, 16, 30));
const opts = { timeZone: "UTC" };
const viaShortcut = d.toLocaleDateString("en-US", opts);
const viaIntl = new Intl.DateTimeFormat("en-US", {
year: "numeric", month: "numeric", day: "numeric", ...opts,
}).format(d);
console.log(viaShortcut === viaIntl); // trueCommon Errors
Calling them with no arguments
d.toLocaleDateString() uses the runtime's default locale and time zone. The output then depends on the machine running the code. A server in Frankfurt and a laptop in Ohio will print different strings for the same instant. Pass the locale and timeZone explicitly whenever the output matters.
Mixing styles with the wrong method
toLocaleDateString accepts dateStyle but throws if you pass timeStyle. toLocaleTimeString has the reverse rule.
const d = new Date(Date.UTC(2024, 6, 4, 16, 30));
console.log(d.toLocaleDateString("en-US", { dateStyle: "medium", timeZone: "UTC" })); // "Jul 4, 2024"
try {
d.toLocaleDateString("en-US", { timeStyle: "short" });
} catch (err) {
console.log(err instanceof TypeError); // true
}If you need both styles, use toLocaleString, which accepts either one.
Using them in a hot loop
Each call does the work of building a formatter: it resolves the locale, the options, and the time zone. Engines may cache some of this, but you can't rely on it. When you format hundreds of values with the same settings, create one Intl.DateTimeFormat and reuse it. A later chapter covers that pattern in detail.
Exercise
A teammate writes event.toLocaleTimeString("en-GB", { dateStyle: "short", timeStyle: "short" }) to show a date and time. What happens, and how should it be fixed?
Show answer
It throws a TypeError, because toLocaleTimeString does not accept dateStyle. Use event.toLocaleString("en-GB", { dateStyle: "short", timeStyle: "short", timeZone: "Europe/London" }) instead. Adding an explicit timeZone keeps the output the same on every machine.
Exercise
What does new Date(Date.UTC(2024, 0, 15)).toLocaleDateString("en-US", { weekday: "long", timeZone: "UTC" }) return, and why doesn't it include the year?
Show answer
It returns "Monday". Because weekday is a date component you specified, the default year, month, and day are not added.