Open In App

JavaScript Intl DateTimeFormat() Constructor

JavaScript Intl.DateTimeFormat Constructor is used for creating Intl.DateTimeFormat objects. This constructor can be called with or without the new keyword

Syntax:



Intl.DateTimeFormat(loc, opt)
new Intl.DateTimeFormat(loc, opt)

Parameter: This constructor has two methods and both are optional.

Returns: This returns a new DateTimeFormat object whose properties differ on whether it is called using new keyword or not.



Below examples illustrate the JavaScript Intl DateTimeFormat() Constructor:

Example 1: In this example, we will create a DateTimeFormat object and use it to format the date object.




const time = new Intl.DateTimeFormat("en", {
    timeStyle: "short",
    dateStyle: "short"
})
var val = new Date();
console.log(time.format(val));

Output: The Date variable was formatted using the format method 

4/3/23, 2:11 PM

Example 2: In this example, we will format the Date object using the constructor.




var val = new Date();
console.log(new Intl.DateTimeFormat("en",{
    hour: "2-digit",
    month: "numeric",
    hourCycle: "h23",
    dayPeriod: "long",
    timeZone: "GMT",
}).format(val));

Output:

4, 08

Supported Browsers:

We have a complete list of JavaScript Intl methods to check those please go through, the JavaScript Intl Reference article.

Article Tags :