Sub topic 9
Formatting Dates with Intl.DateTimeFormat
This chapter assumes you know how a Date stores one instant, how local time and UTC differ, and the Intl basics: locale tags, options objects, and resolvedOptions(). Every example passes timeZone: "UTC" so the output is the same on any machine. The next chapter explains time zones in full.
The working model
Intl.DateTimeFormat combines a locale with a set of options to make a reusable formatter. When you call format(date) on it, the formatter reads the date's time value and returns a string. It does not change the date.
If you pass no options, you get only the numeric year, month, and day:
const event = new Date(Date.UTC(2024, 6, 4, 16, 30)); // 2024-07-04T16:30:00Z
console.log(new Intl.DateTimeFormat("en-US", { timeZone: "UTC" }).format(event)); // "7/4/2024"
console.log(new Intl.DateTimeFormat("en-GB", { timeZone: "UTC" }).format(event)); // "04/07/2024"Two ways to choose the output
Style shortcuts: dateStyle and timeStyle
dateStyle and timeStyle each take "full", "long", "medium", or "short". The locale decides the exact layout, so the result looks right for that locale's readers.
const event = new Date(Date.UTC(2024, 6, 4, 16, 30));
for (const locale of ["en-US", "de-DE", "ja-JP"]) {
const fmt = new Intl.DateTimeFormat(locale, {
dateStyle: "long",
timeStyle: "short",
timeZone: "UTC",
});
console.log(locale, fmt.format(event));
}
// e.g. en-US "July 4, 2024 at 4:30 PM"
// de-DE "4. Juli 2024 um 16:30"
// ja-JP "2024年7月4日 16:30"Component options
If you need control over individual fields, request them one at a time:
| Option | Values |
|---|---|
| `weekday`, `era` | `"long"`, `"short"`, `"narrow"` |
| `year`, `day`, `hour`, `minute`, `second` | `"numeric"`, `"2-digit"` |
| `month` | `"numeric"`, `"2-digit"`, `"long"`, `"short"`, `"narrow"` |
| `hour12` / `hourCycle` | `true`/`false`, or `"h11"`, `"h12"`, `"h23"`, `"h24"` |
| `timeZoneName` | `"short"`, `"long"`, and others |
const event = new Date(Date.UTC(2024, 6, 4, 16, 30));
const fmt = new Intl.DateTimeFormat("en-US", {
weekday: "short",
month: "long",
day: "numeric",
hour: "numeric",
minute: "2-digit",
hour12: false,
timeZone: "UTC",
});
console.log(fmt.format(event)); // e.g. "Thu, July 4, 16:30"
console.log(fmt.resolvedOptions().hourCycle); // "h23"Component options tell the formatter which fields to show. They do not fix the order or the punctuation. The engine picks the closest pattern the locale supports, so you choose the fields and the locale chooses the layout. If you need an exact layout, use formatToParts, which a later chapter covers.
Common errors
- Invalid Date input.
format(new Date("nope"))throwsRangeError: Invalid time value. CheckNumber.isNaN(date.getTime())before you format. - Forgetting the time zone. If you leave out
timeZone, the formatter uses the runtime's zone, so a server and a browser can print different days for the same instant. - Building strings by hand. Joining
getMonth() + 1andgetDate()with slashes ignores the locale's field order. Let the formatter handle it. - Expecting stable text. Spacing and connecting words such as "at" or "," change between engine and data versions. Do not compare formatted strings in tests unless you pin the runtime.
Exercise
What happens when you run new Intl.DateTimeFormat("en-US", { dateStyle: "medium", hour: "numeric" }), and how would you show a medium date with the hour?
Show answer
The constructor throws a TypeError, because dateStyle cannot be combined with component options. Either use { dateStyle: "medium", timeStyle: "short" }, or use only components, such as { year: "numeric", month: "short", day: "numeric", hour: "numeric" }.
Exercise
Write a formatter that prints 2024-07-04T16:30:00Z as a long weekday and long month in German, in UTC.
Show answer
new Intl.DateTimeFormat("de-DE", { weekday: "long", day: "numeric", month: "long", year: "numeric", timeZone: "UTC" }).format(new Date("2024-07-04T16:30:00Z")) returns something like "Donnerstag, 4. Juli 2024".