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.
