More JavaScript answers

by Danny
Published 2 min read

Format dates in JavaScript with toLocaleDateString and Intl.DateTimeFormat instead of a library. Covers locales, time zones, custom parts and ISO strings for machines.

The short answer

#

Use the built-in Intl.DateTimeFormat, or its shortcut toLocaleDateString. Pass a locale and an options object describing what you want to show, and the browser handles month names, order and separators for you. For dates that machines read, use toISOString.


_12
const date = new Date('2026-11-27T14:30:00Z');
_12
_12
date.toLocaleDateString('en-GB', { dateStyle: 'long' });
_12
// "27 November 2026"
_12
_12
new Intl.DateTimeFormat('en-GB', {
_12
weekday: 'short',
_12
day: 'numeric',
_12
month: 'short',
_12
year: 'numeric',
_12
}).format(date);
_12
// "Fri 27 Nov 2026"

Why it happens

#

JavaScript's Date has no format method that takes a pattern like DD/MM/YYYY. Older code filled the gap with libraries such as Moment.js, or by gluing together getDate(), getMonth() and getFullYear() by hand. Both approaches tend to go wrong: the month is zero-based, hand-built strings ignore the reader's locale, and big libraries add weight.

The Intl API, built into every modern browser and Node, solves this properly. You describe the parts you want, and it produces the right output for the locale, including translated month names and the correct order of day, month and year.

How to fix it

#

Use dateStyle and timeStyle for common formats

dateStyle and timeStyle accept "full", "long", "medium" and "short". They cover most interface needs in one option.


_11
const date = new Date('2026-11-27T14:30:00Z');
_11
_11
date.toLocaleString('en-GB', {
_11
dateStyle: 'medium',
_11
timeStyle: 'short',
_11
timeZone: 'Europe/London',
_11
});
_11
// "27 Nov 2026, 14:30"
_11
_11
date.toLocaleDateString('en-US', { dateStyle: 'short' });
_11
// "11/27/26"

You cannot mix dateStyle or timeStyle with individual options like month or weekday. Doing so throws a TypeError.

Pick individual parts for custom formats

For anything else, list the parts you need. Each one takes values such as "numeric", "2-digit", "short" or "long".


_10
const formatter = new Intl.DateTimeFormat('en-GB', {
_10
day: '2-digit',
_10
month: '2-digit',
_10
year: 'numeric',
_10
});
_10
_10
formatter.format(new Date('2026-03-05'));
_10
// "05/03/2026"

Create the formatter once and reuse it when formatting many dates, for example in a table. Building a new one each time is slower.

Set the time zone explicitly

By default, dates are shown in the time zone of the device. On a server, that is often UTC. If the output must match a particular place, pass timeZone with an IANA name.


_10
const options = { dateStyle: 'full', timeStyle: 'short' };
_10
_10
new Intl.DateTimeFormat('en-GB', { ...options, timeZone: 'Asia/Tokyo' })
_10
.format(new Date('2026-11-27T23:30:00Z'));
_10
// "Saturday 28 November 2026 at 08:30"

This matters for server-rendered pages, where the server and the browser can format the same date differently.

Use formatToParts for full control

If you need to style parts separately or build an exact string, formatToParts returns an array of typed pieces.


_10
const parts = new Intl.DateTimeFormat('en-GB', {
_10
day: 'numeric',
_10
month: 'long',
_10
}).formatToParts(new Date('2026-11-27'));
_10
_10
const day = parts.find((part) => part.type === 'day').value; // "27"
_10
const month = parts.find((part) => part.type === 'month').value; // "November"

Use ISO strings for data

For APIs, databases, file names and logs, use a fixed format that does not depend on locale.


_10
new Date().toISOString(); // "2026-11-27T14:30:00.000Z"
_10
new Date().toISOString().slice(0, 10); // "2026-11-27"

Common pitfalls

#
  • Forgetting that getMonth() is zero-based. January is 0, so hand-built strings are often a month out.
  • Parsing new Date('2026-11-27'). A date-only ISO string is treated as UTC midnight, which can show as the previous day in time zones west of UTC.
  • Leaving out the locale. toLocaleDateString() with no arguments uses the device's locale, so tests and screenshots differ between machines.
  • Relying on the exact output in tests. Browsers occasionally change spacing or punctuation between versions, so compare parts or ISO strings instead.
  • Formatting relative times by hand. Use Intl.RelativeTimeFormat for "3 days ago". For date arithmetic, see the newer Temporal API.
#

Was this resource helpful?