0Pricing
JavaScript Academy · Lesson

Formatting Dates and Times

Display dates with Intl.DateTimeFormat.

Formatting Dates and Times is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intl.DateTimeFormat

Intl.DateTimeFormat formats Date objects according to a locale, replacing brittle manual string building.

const dtf = new Intl.DateTimeFormat("en-US");
console.log(dtf.format(new Date("2026-05-29"))); // "5/29/2026"

Locale Differences

Date order differs by region: US is month/day/year, much of Europe is day/month/year.

const d = new Date("2026-05-29");
console.log(new Intl.DateTimeFormat("en-US").format(d)); // 5/29/2026
console.log(new Intl.DateTimeFormat("en-GB").format(d)); // 29/05/2026

Date Style Presets

dateStyle offers "full", "long", "medium", "short" presets for quick, readable output.

const d = new Date("2026-05-29");
console.log(new Intl.DateTimeFormat("en-US", { dateStyle: "full" }).format(d));
// "Friday, May 29, 2026"

Time Style

timeStyle works the same way for the time portion and can combine with dateStyle.

const d = new Date("2026-05-29T14:30:00");
console.log(new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "short" }).format(d));
// "May 29, 2026, 2:30 PM"

Custom Component Options

For fine control, specify components like year, month, day, weekday individually.

const d = new Date("2026-05-29");
const f = new Intl.DateTimeFormat("en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric" });
console.log(f.format(d)); // "Friday, May 29, 2026"

Month Formats

month accepts "numeric", "2-digit", "short", "long", and "narrow".

const d = new Date("2026-05-29");
console.log(new Intl.DateTimeFormat("en-US", { month: "long" }).format(d));  // "May"
console.log(new Intl.DateTimeFormat("en-US", { month: "short" }).format(d)); // "May"

12 vs 24 Hour

hour12: true/false overrides the locale default for clock format.

const d = new Date("2026-05-29T20:05:00");
console.log(new Intl.DateTimeFormat("en-US", { hour: "numeric", minute: "2-digit", hour12: false }).format(d));
// "20:05"

Time Zones

Pass a timeZone IANA name to render a date in any zone, independent of the runtime's local zone.

const d = new Date("2026-05-29T12:00:00Z");
console.log(new Intl.DateTimeFormat("en-US", { timeStyle: "long", timeZone: "Asia/Tokyo" }).format(d));

formatToParts

formatToParts returns an array of typed tokens, letting you style or rearrange individual pieces.

const parts = new Intl.DateTimeFormat("en-US").formatToParts(new Date("2026-05-29"));
console.log(parts.map((p) => p.type + ":" + p.value).join(" "));

Formatting Ranges

formatRange(start, end) renders a date range compactly, merging shared components.

const f = new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric" });
console.log(f.formatRange(new Date("2026-05-01"), new Date("2026-05-05")));
// "May 1 – 5"

Non-Gregorian Calendars

The calendar option supports systems like "islamic", "hebrew", or "japanese".

const d = new Date("2026-05-29");
console.log(new Intl.DateTimeFormat("en-US", { calendar: "japanese", dateStyle: "long" }).format(d));

Quick Check

Test date formatting.

Recap: Dates and Times

You formatted dates and times with Intl.DateTimeFormat, used dateStyle/timeStyle presets and granular component options, handled time zones, 12/24-hour clocks, formatToParts, ranges, and alternate calendars. Next: relative time and plurals.

Frequently asked questions

Is the “Formatting Dates and Times” lesson free?

Yes — the full text of “Formatting Dates and Times” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Formatting Dates and Times”?

Display dates with Intl.DateTimeFormat. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Formatting Dates and Times” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Formatting Numbers and Currency
  2. Formatting Dates and Times
  3. Relative Time and Plurals
  4. Locale-Aware Sorting
← Back to JavaScript Academy