Course contentsShow
← Web Development
Lesson 2758 of 2,98249. Dates, Times, and Numbers on the WebFree lesson

Intl.DateTimeFormat Constructor

Understand how to create reusable DateTimeFormat instances with options for consistent date and time formatting.

What You'll Learn

You'll learn how to create reusable Intl.DateTimeFormat instances that apply consistent formatting rules to multiple dates throughout your application.

The Core Idea

While toLocaleString() is convenient for one-off formatting, the Intl.DateTimeFormat constructor lets you create a formatter object once and reuse it many times. Think of it like creating a template or stamp—you define the formatting rules upfront, then apply them to as many dates as you need without repeating those options every time.

This pattern is more performant when formatting multiple dates and makes your code more maintainable by centralizing format definitions.

Creating a DateTimeFormat Instance

The constructor accepts a locale string and an options object:

const formatter = new Intl.DateTimeFormat('en-US', {
  dateStyle: 'long',
  timeStyle: 'short',
  timeZone: 'America/New_York'
});

const date1 = new Date('2024-01-15T14:30:00Z');
const date2 = new Date('2024-03-22T09:15:00Z');

console.log(formatter.format(date1)); // "January 15, 2024 at 9:30 AM"
console.log(formatter.format(date2)); // "March 22, 2024 at 5:15 AM"

Benefits of Reusable Formatters

Creating formatters once and reusing them is especially valuable for lists, tables, or repeated operations:

const eventFormatter = new Intl.DateTimeFormat('en-GB', {
  day: '2-digit',
  month: 'short',
  year: 'numeric',
  hour: '2-digit',
  minute: '2-digit'
});

const events = [new Date('2024-06-10'), new Date('2024-07-20')];
const formattedEvents = events.map(date => eventFormatter.format(date));
// ["10 Jun 2024, 00:00", "20 Jul 2024, 00:00"]

Key Takeaway: Use the Intl.DateTimeFormat constructor to create reusable formatter instances that ensure consistent date/time presentation across your application while improving performance and maintainability.