Formatting a date
suggest changeUse Intl.DateTimeFormat for locale-aware date and time output. Specify the locale and time zone when output must be stable across machines:
const formatter = new Intl.DateTimeFormat('en-GB', {
dateStyle: 'medium',
timeZone: 'Europe/London',
});
formatter.format(new Date('2036-01-24T12:00:00Z')); // '24 Jan 2036'
Create the formatter once when formatting many values; this avoids repeatedly resolving locale data and options.
Individual fields provide more control:
const dateTimeFormatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
timeZone: 'America/New_York',
timeZoneName: 'short',
});
console.log(dateTimeFormatter.format(new Date()));
Use formatToParts() when the application needs locale-correct fields inside custom markup rather than a fully assembled string:
const parts = formatter.formatToParts(new Date());
const year = parts.find(part => part.type === 'year')?.value;
Avoid manually maintained month-name arrays and string padding for user-facing output. They omit locale rules, calendars, numbering systems, and time-zone transitions. For a machine-readable UTC timestamp, use date.toISOString() instead.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents