Formatting Dates

Updated · 7 min read
mediumES2026javascriptdateintlformattingtime-zones

Date formatting is turning a date into text for a particular audience. People in the US expect 1/5/2026 or January 5, 2026; a database or API expects 2026-01-05T16:00:00.000Z; a notification reads best as "in 3 days". JavaScript's built-in Intl.DateTimeFormat (and the shortcut toLocaleDateString) handles human formats for any language and time zone, toISOString handles machine formats, and Intl.RelativeTimeFormat handles "ago" and "in". Building date strings by hand is almost never needed.

In One Minute

  • For people: date.toLocaleDateString("en-US", options) or a reusable new Intl.DateTimeFormat("en-US", options).
  • Options choose the parts and their style: { dateStyle: "long" }, or { month: "short", day: "numeric", year: "numeric" }.
  • Add timeZone: "America/Los_Angeles" to show a date in a specific zone, whatever the reader's computer says.
  • For storage and APIs: date.toISOString() (always UTC).
  • For relative times: new Intl.RelativeTimeFormat("en-US", { numeric: "auto" }).format(-1, "day") gives "yesterday".

As in the dates lesson, these examples ran in the America/Chicago time zone, except where a timeZone option is given.

Built-In Methods

methods.js▶ Try it
const d = new Date("2026-01-05T16:30:00");
 
console.log(d.toISOString());
console.log(d.toDateString());
console.log(d.toLocaleDateString("en-US"));
console.log(d.toLocaleTimeString("en-US"));
console.log(d.toLocaleString("en-US"));
console.log(d.toJSON());
Output
2026-01-05T22:30:00.000Z
Mon Jan 05 2026
1/5/2026
4:30:00 PM
1/5/2026, 4:30:00 PM
2026-01-05T22:30:00.000Z
MethodOutputUse for
toISOString()2026-01-05T22:30:00.000ZStorage, APIs, sorting (always UTC)
toJSON()Same as toISOString()Used automatically by JSON.stringify
toLocaleDateString(locale, opts)1/5/2026Showing the date to people
toLocaleTimeString(locale, opts)4:30:00 PMShowing the time to people
toLocaleString(locale, opts)1/5/2026, 4:30:00 PMBoth
toDateString(), toString()Mon Jan 05 2026Debugging only (fixed English format)

Intl.DateTimeFormat Options

Preset Styles

dateStyle and timeStyle pick a complete, conventional format:

styles.js▶ Try it
const d = new Date("2026-01-05T16:30:00");
 
for (const style of ["short", "medium", "long", "full"]) {
  console.log(style.padEnd(7), d.toLocaleDateString("en-US", { dateStyle: style }));
}
console.log(d.toLocaleString("en-US", { dateStyle: "medium", timeStyle: "short" }));
Output
short   1/5/26
medium  Jan 5, 2026
long    January 5, 2026
full    Monday, January 5, 2026
Jan 5, 2026, 4:30 PM

Choosing Parts

For anything else, request the parts you want:

OptionValuesExample
weekday"long", "short", "narrow"Monday, Mon, M
year"numeric", "2-digit"2026, 26
month"numeric", "2-digit", "long", "short"1, 01, January, Jan
day"numeric", "2-digit"5, 05
hour, minute, second"numeric", "2-digit"4, 04
hour12true, false4 PM or 16
timeZoneAn IANA zone name"America/New_York"
timeZoneName"short", "long"CST, Central Standard Time
parts.js▶ Try it
const d = new Date("2026-01-05T16:30:00");
 
console.log(d.toLocaleDateString("en-US", { month: "short", day: "numeric" }));
console.log(d.toLocaleDateString("en-US", { month: "2-digit", day: "2-digit", year: "numeric" }));
console.log(d.toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric" }));
console.log(d.toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit" }));
console.log(d.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", hour12: false }));
Output
Jan 5
01/05/2026
Monday, January 5
4:30 PM
16:30

Create the formatter once with new Intl.DateTimeFormat(...) and call .format(date) many times when formatting a whole list: it is faster than calling toLocaleDateString with options for every item.

Time Zones

A Date is a moment, and the same moment is a different clock time in different places. The timeZone option shows it in any zone:

zones.js▶ Try it
const meeting = new Date("2026-01-05T22:30:00Z");
const fmt = (timeZone) =>
  meeting.toLocaleString("en-US", {
    timeZone, month: "short", day: "numeric", year: "numeric",
    hour: "numeric", minute: "2-digit", timeZoneName: "short",
  });
 
console.log(fmt("America/Chicago"));
console.log(fmt("America/Los_Angeles"));
console.log(fmt("America/New_York"));
console.log(fmt("UTC"));
console.log(fmt("Asia/Kolkata"));
Output
Jan 5, 2026, 4:30 PM CST
Jan 5, 2026, 2:30 PM PST
Jan 5, 2026, 5:30 PM EST
Jan 5, 2026, 10:30 PM UTC
Jan 6, 2026, 4:00 AM GMT+5:30

Navika in Dallas and Srihitha in San Francisco see the same payment deadline at different clock times. Showing the zone name avoids confusion. Note that the example lists the parts one by one: dateStyle and timeStyle can't be combined with individual options such as timeZoneName (that throws TypeError: Invalid option). Use IANA zone names (America/Chicago), not abbreviations (CST), which are ambiguous and don't handle daylight saving.

Formatting to Parts

formatToParts returns the pieces separately, so you can style them (for example, bold the day) without parsing the string:

to-parts.js▶ Try it
const parts = new Intl.DateTimeFormat("en-US", { month: "long", day: "numeric", year: "numeric" })
  .formatToParts(new Date("2026-01-05T12:00:00"));
 
console.log(parts.map((p) => `${p.type}=${p.value}`).join(" | "));
const day = parts.find((p) => p.type === "day").value;
console.log(day);
Output
month=January | literal=  | day=5 | literal=,  | year=2026
5

Relative Times

Intl.RelativeTimeFormat turns a number and a unit into natural phrases:

relative.js▶ Try it
const rtf = new Intl.RelativeTimeFormat("en-US", { numeric: "auto" });
 
console.log(rtf.format(-1, "day"));
console.log(rtf.format(1, "day"));
console.log(rtf.format(-3, "day"));
console.log(rtf.format(2, "week"));
console.log(rtf.format(0, "month"));
 
function dueIn(days) {
  return `Due ${rtf.format(days, "day")}`;
}
console.log(dueIn(5), "|", dueIn(-2));
Output
yesterday
tomorrow
3 days ago
in 2 weeks
this month
Due in 5 days | Due 2 days ago

numeric: "auto" uses words like "yesterday" and "tomorrow" where they exist; numeric: "always" would say "1 day ago". You calculate the number yourself (for example, calendar days between two dates); the formatter only handles the wording.

In Practice

The Fee Tracker's payment list shows each payment's date for a US reader, the school's office deadline in the school's own time zone, and a relative due date:

list.js▶ Try it
const dateFmt = new Intl.DateTimeFormat("en-US", { dateStyle: "medium" });
const officeFmt = new Intl.DateTimeFormat("en-US", {
  timeZone: "America/Chicago", weekday: "short", hour: "numeric", minute: "2-digit", timeZoneName: "short",
});
const rtf = new Intl.RelativeTimeFormat("en-US", { numeric: "auto" });
 
const payments = [
  { name: "Navika", paidAt: "2026-01-05T15:00:00Z" },
  { name: "Srihitha", paidAt: "2026-01-12T23:45:00Z" },
];
for (const p of payments) {
  console.log(`${p.name.padEnd(9)} ${dateFmt.format(new Date(p.paidAt))}`);
}
 
const officeCloses = new Date("2026-01-30T23:00:00Z");
console.log("Office closes:", officeFmt.format(officeCloses));
console.log("Next payment:", rtf.format(3, "day"));
Output
Navika    Jan 5, 2026
Srihitha  Jan 12, 2026
Office closes: Fri, 5:00 PM CST
Next payment: in 3 days

The data stays in ISO UTC strings; formatting happens only for display, with the time zone chosen on purpose.

Common Mistakes

Building date strings by hand. d.getMonth() + "/" + d.getDate() forgets the +1 and the year. Use Intl.DateTimeFormat.

Storing formatted dates. "1/5/2026" can't be sorted or reliably parsed. Store ISO strings.

Omitting the locale. Output then depends on the reader's settings. Pass "en-US" when you need US format.

Using time zone abbreviations as timeZone. Use IANA names like "America/New_York".

Practice

Challenge: Write receiptDate(isoString) that returns text like Monday, January 5, 2026 at 4:30 PM PST, always in the Los Angeles time zone.

Show solution
receipt-date.js▶ Try it
const zone = "America/Los_Angeles";
const dayFmt = new Intl.DateTimeFormat("en-US", { timeZone: zone, dateStyle: "full" });
const timeFmt = new Intl.DateTimeFormat("en-US", {
  timeZone: zone, hour: "numeric", minute: "2-digit", timeZoneName: "short",
});
 
function receiptDate(isoString) {
  const d = new Date(isoString);
  return `${dayFmt.format(d)} at ${timeFmt.format(d)}`;
}
 
console.log(receiptDate("2026-01-06T00:30:00Z"));
Output
Monday, January 5, 2026 at 4:30 PM PST

Quick check: Formatting dates

3 questions
1Which method should you use to store a date in a database or send it to an API?
2What does the timeZone option do?
3What does new Intl.RelativeTimeFormat("en-US", { numeric: "auto" }).format(-1, "day") return?

Key Takeaways

WantCode
1/5/2026d.toLocaleDateString("en-US")
January 5, 2026{ dateStyle: "long" }
4:30 PM{ hour: "numeric", minute: "2-digit" }
In another zone{ timeZone: "America/New_York" }
3 days agortf.format(-3, "day")

Was this tutorial helpful?

Lesson 56 of 139