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

FormatRange for Date Intervals

Format a range between two dates with formatRange, automatically handling shared components efficiently.

FormatRange for Date Intervals

You'll learn how to format a range between two dates efficiently using formatRange(), which automatically handles shared components like the year or month.

The Core Idea

When displaying date ranges (like "January 15–20, 2024"), you don't want to repeat information unnecessarily. The formatRange() method on Intl.DateTimeFormat instances intelligently compares two dates and only shows what differs between them, following locale conventions.

Think of it like giving directions: you wouldn't say "Go to 123 Main Street, Boston and then to 456 Main Street, Boston"—you'd say "Go to 123 and 456 Main Street, Boston." formatRange() applies this same efficiency to dates.

How It Works

Once you've created a DateTimeFormat instance with your desired options, call formatRange(startDate, endDate):

const formatter = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
});

const start = new Date(2024, 0, 15); // Jan 15
const end = new Date(2024, 0, 20);   // Jan 20

console.log(formatter.formatRange(start, end));
// "January 15 – 20, 2024"

Notice how the year and month appear only once because they're shared. If the dates span different months:

const start = new Date(2024, 0, 28); // Jan 28
const end = new Date(2024, 1, 3);    // Feb 3

console.log(formatter.formatRange(start, end));
// "January 28 – February 3, 2024"

The method automatically adjusts based on what's different, following locale-specific rules for separators and ordering.

Key Takeaway: Use formatRange() to display date intervals efficiently—it automatically collapses shared components (year, month) and formats the range according to locale conventions, creating natural-looking date ranges without manual string manipulation.