format
coding
date
javascript

How do I format a date in JavaScript?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Formatting dates in JavaScript is easy to do badly and slightly harder to do correctly. The core issue is that a Date object represents a specific instant in time, while formatting is about how that instant should be displayed for a locale, a time zone, and a specific output pattern. The safest answer for most applications is Intl.DateTimeFormat.

Use Intl.DateTimeFormat for Real Formatting

The modern built-in API for date formatting is Intl.DateTimeFormat.

javascript
1const date = new Date("2025-03-11T18:30:00Z");
2
3const formatter = new Intl.DateTimeFormat("en-US", {
4  year: "numeric",
5  month: "long",
6  day: "numeric",
7  timeZone: "UTC",
8});
9
10console.log(formatter.format(date));

This is usually better than manually piecing together month names, locale rules, or punctuation.

A major advantage is that locale and time zone are explicit. That avoids the “works on my machine” problem where one developer sees one format and another sees something different.

toLocaleDateString() Is a Shortcut

If you want a simpler built-in method, toLocaleDateString() is a thin convenience wrapper around the same internationalization system.

javascript
1const date = new Date("2025-03-11T18:30:00Z");
2
3console.log(
4  date.toLocaleDateString("en-GB", {
5    year: "numeric",
6    month: "short",
7    day: "2-digit",
8    timeZone: "UTC",
9  })
10);

This is fine for many UI cases. Just remember that if you omit locale or time zone, the output becomes environment-dependent.

Manual Formatting for Fixed Machine-Friendly Output

Sometimes you do not want a human-localized display. You want a fixed technical format such as YYYY-MM-DD.

In that case, manual formatting is reasonable.

javascript
1function formatYMD(date) {
2  const year = date.getFullYear();
3  const month = String(date.getMonth() + 1).padStart(2, "0");
4  const day = String(date.getDate()).padStart(2, "0");
5  return `${year}-${month}-${day}`;
6}
7
8const date = new Date(2025, 2, 11);
9console.log(formatYMD(date));

This is predictable and easy to test.

If the date should be interpreted in UTC rather than local time, use the UTC getters instead:

javascript
1function formatUTCYMD(date) {
2  const year = date.getUTCFullYear();
3  const month = String(date.getUTCMonth() + 1).padStart(2, "0");
4  const day = String(date.getUTCDate()).padStart(2, "0");
5  return `${year}-${month}-${day}`;
6}

That distinction matters more than many developers realize.

toISOString() Is Useful, but Different

If you need a standard machine timestamp, toISOString() is excellent.

javascript
const date = new Date("2025-03-11T18:30:00Z");
console.log(date.toISOString());

But toISOString() always outputs UTC in ISO 8601 form. It is not a general-purpose “format any date the way I want” API.

So use it for:

  • API payloads
  • logs
  • database interchange
  • machine-stable timestamps

Do not use it when the requirement is “show a localized date to the user.”

Time Zone Choice Changes the Output

Date formatting bugs often come from time zone assumptions, not from the formatting method itself.

javascript
1const date = new Date("2025-03-11T23:30:00Z");
2
3console.log(
4  new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeZone: "UTC" }).format(date)
5);
6
7console.log(
8  new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeZone: "America/Toronto" }).format(date)
9);

The same instant can display as different calendar dates in different time zones. If you do not choose the zone intentionally, subtle production bugs appear around midnight.

When Libraries Help

For plain formatting, built-in APIs are often enough. Libraries become more attractive when you also need:

  • parsing many custom formats
  • date arithmetic across time zones
  • immutable date-time objects
  • business-calendar logic

That is why many teams use libraries for broader date handling, not because built-in formatting is missing.

Common Pitfalls

The biggest pitfall is formatting dates without specifying the intended locale or time zone.

Another issue is using local getters such as getFullYear() when the business rule is actually UTC-based.

Developers also often use toISOString() for user-facing display even though it is a machine-oriented UTC format.

Finally, manual formatting is fine for fixed formats, but it is a poor replacement for locale-aware display rules.

Summary

  • Use Intl.DateTimeFormat for most real user-facing date formatting in JavaScript.
  • 'toLocaleDateString() is a convenient wrapper when you want similar behavior with less setup.'
  • For fixed formats such as YYYY-MM-DD, manual formatting can be a clean solution.
  • 'toISOString() is for standardized machine timestamps, not localized UI output.'
  • Always decide whether the formatting rule is local-time or UTC-based before writing the code.

Course illustration
Course illustration

All Rights Reserved.