Sub topic 11
The toLocale* Shortcuts and When to Use Them
This chapter assumes you already know how Intl.DateTimeFormat works: locale tags, the dateStyle and timeStyle options, component options such as month: "long", and the timeZone option. It also assumes you know that a Date stores a single instant, and that local time and UTC are two ways of viewing it. Here we look at the three methods on Date.prototype that wrap the same formatting engine in a single call, and at the places where their behavior differs from the full formatter.
Three methods, one engine
Every Date has three locale-aware methods:
| Method | Default output when no component options are given | Typical use |
|---|---|---|
| `toLocaleString()` | Date and time | Timestamps in a UI |
| `toLocaleDateString()` | Date only (year, month, day) | Birthdays, due dates |
| `toLocaleTimeString()` | Time only (hour, minute, second) | Clock displays, log times |
All three accept the same two arguments as the Intl.DateTimeFormat constructor, (locales, options). Internally, each call builds a formatter from those arguments and formats the date with it. In engines that support Intl, which today means all mainstream browsers, Node.js, Deno, and Bun, the following lines produce identical strings:
const instant = new Date(Date.UTC(2024, 6, 4, 16, 30));
const options = { dateStyle: "medium", timeStyle: "short", timeZone: "Europe/Paris" };
const viaShortcut = instant.toLocaleString("fr-FR", options);
const viaFormatter = new Intl.DateTimeFormat("fr-FR", options).format(instant);
console.log(viaShortcut); // e.g. "4 juil. 2024, 18:30"
console.log(viaShortcut === viaFormatter); // trueSo the shortcuts have the same expressive power as the formatter. They differ in how defaults are filled in, which option combinations they accept, how they handle errors, and what they cost when called repeatedly.
How the defaults work
When you pass no component options, each method adds its own numeric defaults:
const d = new Date(Date.UTC(2024, 0, 15, 9, 5, 7));
const opts = { timeZone: "UTC" };
console.log(d.toLocaleString("en-US", opts)); // "1/15/2024, 9:05:07 AM"
console.log(d.toLocaleDateString("en-US", opts)); // "1/15/2024"
console.log(d.toLocaleTimeString("en-US", opts)); // "9:05:07 AM"
console.log(d.toLocaleDateString("de-DE", opts)); // "15.1.2024"
console.log(d.toLocaleTimeString("ja-JP", opts)); // "9:05:07"timeZone does not count as a component, so it never switches the defaults off. A component option such as month or hour does switch them off, but only for the group the method looks at. toLocaleDateString checks the date components (weekday, year, month, day). toLocaleTimeString checks the time components (hour, minute, second, and similar). This produces a surprising result:
const d = new Date(Date.UTC(2024, 0, 15, 9, 5));
// No date component given, so the date defaults are still added.
console.log(d.toLocaleDateString("en-US", { hour: "numeric", timeZone: "UTC" }));
// e.g. "1/15/2024, 9 AM"
// A date component given, so only what you asked for is shown.
console.log(d.toLocaleDateString("en-US", { month: "long", timeZone: "UTC" }));
// "January"The method name does not stop time fields from appearing in toLocaleDateString's output. It only decides which defaults get added when you leave that group empty.
The style-option trap
The dateStyle and timeStyle options get stricter treatment. toLocaleDateString throws if you pass timeStyle, and toLocaleTimeString throws if you pass dateStyle. Only toLocaleString accepts both.
const d = new Date();
console.log(d.toLocaleDateString("en-GB", { dateStyle: "long" })); // fine
console.log(d.toLocaleTimeString("en-GB", { timeStyle: "short" })); // fine
try {
d.toLocaleDateString("en-GB", { timeStyle: "short" });
} catch (err) {
console.log(err.name); // "TypeError"
}As with Intl.DateTimeFormat, combining a style option with a component option such as { dateStyle: "long", weekday: "long" } also throws a TypeError, in every method.
Invalid dates: a string versus an exception
The shortcuts and the formatter handle an Invalid Date differently:
const bad = new Date("not a date");
console.log(bad.toLocaleString()); // "Invalid Date"
console.log(bad.toLocaleDateString()); // "Invalid Date"
try {
new Intl.DateTimeFormat("en-US").format(bad);
} catch (err) {
console.log(err.name); // "RangeError"
}With a shortcut, a parsing bug shows up in your UI as the English text "Invalid Date", whatever locale was requested, and nothing fails loudly. With the formatter, the bug throws at the point where it happens. Choose the behavior you want, and validate input with Number.isNaN(date.getTime()) in either case.
Calling with no arguments
date.toLocaleString() with no arguments uses the runtime's default locale and default time zone. That is right for a quick display in a user's own browser and wrong almost everywhere else:
- Servers format in the server's locale and zone, often
en-USandUTC, not the user's. - Tests pass on one developer's machine and fail in CI when the locale or
TZsetting differs. - Server-side rendering can produce different text on the server and in the browser, which causes hydration mismatch warnings in frameworks such as React.
- Logs become hard to sort or parse. Use
toISOString()for logs.
Pass an explicit locale (or undefined when you deliberately want the user's default) and an explicit timeZone whenever the output must be predictable:
const d = new Date(Date.UTC(2024, 11, 31, 23, 30));
// Predictable everywhere:
console.log(d.toLocaleString("en-US", { timeZone: "UTC" })); // "12/31/2024, 11:30:00 PM"
console.log(d.toLocaleString("en-US", { timeZone: "America/Chicago" })); // "12/31/2024, 5:30:00 PM"
// User's locale, but a fixed zone:
console.log(d.toLocaleDateString(undefined, { timeZone: "UTC", dateStyle: "medium" }));Also keep toLocaleString apart from toString and toDateString. Those two print an English, implementation-defined format such as "Tue Dec 31 2024" and ignore locales.
Cost when called repeatedly
Each shortcut call has to resolve the locale and options and build formatting data, much like calling new Intl.DateTimeFormat(...). Some engines cache the formatter for the plain no-argument call, but you cannot rely on a cache once you pass custom options. For a single label this cost does not matter. For a table with ten thousand rows, build one formatter and reuse it:
const rows = Array.from({ length: 10000 }, (_, i) => new Date(Date.UTC(2024, 0, 1 + (i % 365))));
const opts = { dateStyle: "medium", timeZone: "UTC" };
console.time("toLocaleDateString");
rows.map((d) => d.toLocaleDateString("en-US", opts));
console.timeEnd("toLocaleDateString");
console.time("shared formatter");
const fmt = new Intl.DateTimeFormat("en-US", opts);
rows.map((d) => fmt.format(d));
console.timeEnd("shared formatter");In most engines the shared formatter runs many times faster. A later chapter covers reuse patterns in detail.
What the shortcuts cannot do
The shortcuts return a finished string only. They give you no access to:
formatToParts, for styling or rearranging individual fields.formatRange, for spans such as "Jul 4 – 6, 2024".resolvedOptions, for checking which locale, calendar, and time zone were actually used.
If you need any of these, you need an Intl.DateTimeFormat instance anyway.
Decision guide
| Situation | Choose |
|---|---|
| One-off label, user's own browser | `toLocaleDateString` / `toLocaleTimeString` |
| Many values in a loop or list | Shared `Intl.DateTimeFormat` |
| Need parts, ranges, or resolved options | `Intl.DateTimeFormat` |
| Bad input should fail loudly | `Intl.DateTimeFormat` (throws `RangeError`) |
| Server, tests, SSR | Either one, always with explicit locale and `timeZone` |
| Logs and machine-readable output | `toISOString()`, not a locale method |
The related methods Number.prototype.toLocaleString and Array.prototype.toLocaleString follow the same pattern. The number method is a one-call form of Intl.NumberFormat. The array method calls toLocaleString on each element and joins the results with a separator that depends on the implementation.
Exercise
A helper calls date.toLocaleTimeString("en-US", { dateStyle: "short", timeStyle: "short" }). What happens, and how would you fix it so the output shows only the time?
Show answer
The call throws a TypeError, because toLocaleTimeString rejects the dateStyle option. Remove dateStyle and pass { timeStyle: "short" }. If you need both the date and the time, call toLocaleString with both style options instead.
Exercise
What does new Date(Date.UTC(2024, 2, 1, 14)).toLocaleDateString("en-US", { minute: "2-digit", timeZone: "UTC" }) include, and why?
Show answer
It includes the date and the minute, for example "3/1/2024, 00". The minute is a time component, so it does not count as a date component, and toLocaleDateString still adds its default year, month, and day. The output shows the date plus the minute you asked for.
Exercise
A user profile page shows "Invalid Date" in a German locale and no error is logged. Which API produced this, and what change would surface the bug earlier?
Show answer
A toLocale* shortcut produced it. These methods return the literal English string "Invalid Date" for a Date whose time value is NaN. Either check Number.isNaN(date.getTime()) after parsing and report the error, or format with an Intl.DateTimeFormat instance, whose format method throws a RangeError for an invalid date.