Thursday, January 9, 2025
Google search engine
HomeLanguagesJavascriptJavaScript Intl DateTimeFormat() Constructor

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.

  • loc: This is a String or an array of Strings with the following values allowed:
    • nu: It Specifies the numbering system to be followed
    • ca: It specifies the calendar to be followed
    • hc: It specifies the hour cycle format to be followed
  • opt: This parameter contains other properties such as datestyle, timestyle, dayperiod, era etc.

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.

Javascript




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.

Javascript




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:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments