JavaScript Intl.DateTimeFormat() Constructor

JavaScript Int.DateTimeFormat() constructor is “used to create objects for Intl.DateTimeFormat(), which can be called with or without a new keyword.”

Syntax

new Intl.DateTimeFormat(locales, options)
Intl.DateTimeFormat(locales, options)

Parameters

locales(optional): A string with a BCP 47 language tag or an array of such strings.

  • nu: It defines the numbering system
  • ca:  It defines the calendar
  • hc: It defines the hour cycle format

options(optional): An object that can have properties like datestyle, calender, dayperiod, formatMatcher, timezone, and many more.

Return Value

It returns a new Intl.DateTimeFormat object. 

Example 1: Using Intl.DateTimeFormat() Constructor

let dateTime = new Date();

let options = {
 year: 'numeric',
 month: 'long',
 day: 'numeric',
 weekday: 'long',
 hour: 'numeric',
 minute: 'numeric',
 second: 'numeric',
 timeZoneName: 'short'
};

const formatter = new Intl.DateTimeFormat('en', options).format(dateTime);

console.log(formatter);

Output

Thursday, August 31, 2023 at 11:38:42 AM GMT+5:30

Example 2: Using Customized Date Format

const date = new Date('2023-08-31');
const options = { year: 'numeric', month: 'long', day: 'numeric' };
const formattedDate = new Intl.DateTimeFormat('en-US', options).format(date);
console.log(formattedDate);

Output

August 31, 2023

Browser Compatibility

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

That’s it!

Related posts

JavaScript Intl.DateTimeFormat.format() Method

JavaScript Intl DateTimeFormat formatRange() Method

Leave a Comment