JavaScript Intl.DateTimeFormat.format() Method

JavaScript Intl.DateTimeFormat.format() is “used to format a date according to the locale and formatting options of this Intl.DateTimeFormat object.”

Syntax

Intl.dateTimeFormat.format(date)

Parameters

date: This parameter holds the date which needs to format.

Example 1: Basic Date Formatting

// Default locale and default options

let date = new Date();
let formatter = new Intl.DateTimeFormat();

console.log(formatter.format(date));

Output

8/15/2023

Example 2: Formatting with a Specific Locale and Options

// German locale, with day, month, and year
let date = new Date();

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

console.log(formatter.format(date));

Output

15. August 2023

Example 3: Using Multiple Locales and Specific Options

// Try using French locale first, then fallback to English if not available
// Also, show weekday, day, month, and year
let date = new Date();

let formatter = new Intl.DateTimeFormat(['fr-FR', 'en-US'], {
  weekday: 'long',
  year: 'numeric',
  month: 'short',
  day: '2-digit'
});

console.log(formatter.format(date));

Output

mardi 15 août 2023

These examples demonstrate the primary usage of Intl.DateTimeFormat.format() with different configurations. Adjusting the locale and options allows one to achieve various date formats suitable for different scenarios.

Browser Compatibility

  1. Google Chrome 24 and above
  2. Firefox 29 and above
  3. Opera 15 and above
  4. Edge 12 and above
  5. IE 11 and above
  6. Safari 10 and above

That’s it!

Related posts

JavaScript Date getDay() Method

How to Add Days to Date in JavaScript

How to Compare Two Dates in JavaScript

How to Subtract Days from a Date in JavaScript

How to Format Dates in JavaScript

Leave a Comment