Course contentsShow
← JavaScript
Lesson 2214 of 2,72346. InternationalizationFree lesson

Formatting Date Objects

Use the format method to convert Date objects into locale-aware strings based on your configuration.

Formatting Date Objects

You'll learn how to convert JavaScript Date objects into beautifully formatted, locale-aware strings using Intl.DateTimeFormat.

The Core Idea

Once you've configured an Intl.DateTimeFormat instance with your desired locale and options (styles, components, time zones), you call its format() method with a Date object. This method returns a formatted string that respects all your settings—locale, time zone, which components to show, and how to display them.

Think of it like feeding a raw date through a personalized template machine: you configure the machine once, then pass dates through it repeatedly to get consistent, localized output.

Using the format() Method

const formatter = new Intl.DateTimeFormat('en-US', {
  dateStyle: 'full',
  timeStyle: 'short'
});

const date = new Date('2024-03-15T14:30:00');
const formatted = formatter.format(date);
console.log(formatted); // "Friday, March 15, 2024 at 2:30 PM"

The format() method takes a Date object and applies your configuration. You can reuse the same formatter for multiple dates:

const shortFormatter = new Intl.DateTimeFormat('de-DE', {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit'
});

console.log(shortFormatter.format(new Date('2024-01-05'))); // "05.01.2024"
console.log(shortFormatter.format(new Date('2024-12-25'))); // "25.12.2024"

Why This Matters

Separating configuration from formatting means you can create formatters once and apply them efficiently across your application. The heavy lifting—locale data, time zone calculations—happens when you create the instance. The format() call is fast.

Key Takeaway: Use the format() method on an Intl.DateTimeFormat instance to transform Date objects into locale-aware, human-readable strings that respect all your configuration choices—reuse the formatter for consistent results across multiple dates.