JavaScript Intl DateTimeFormat format() Method
Last Updated :
09 Sep, 2024
The Intl.DateTimeFormat format() method in JavaScript formats dates and times according to a specified locale and options. It allows you to customize the output with settings for timezone, date, time, and more, making it ideal for creating locale-sensitive and readable date-time strings.
Syntax:
Intl.dateTimeFormat.format( date )
Parameters: This method accepts a single parameter as mentioned above and described below:
- date: This parameter holds the date which needs to format.
The below examples illustrate the Intl.DateTimeFormat.prototype.format() method in JavaScript:
Example 1: In this example, we will print the specific dates, and days in three different languages and one modified pattern.
javascript
const Geeks = {
weekday: 'long', year:
'numeric', month: 'long', day: 'numeric'
};
const dateformat = new Date(1997, 06, 30);
const dateTimeFormat4 = new Intl.DateTimeFormat('hi', Geeks);
console.log(dateTimeFormat4.format(dateformat));
const dateTimeFormat2 = new Intl.DateTimeFormat('en-GB', Geeks);
console.log(dateTimeFormat2.format(dateformat));
const dateTimeFormat1 = new Intl.DateTimeFormat('sr-RS', Geeks);
console.log(dateTimeFormat1.format(dateformat));
const dateTimeFormat3 = new Intl.DateTimeFormat('en-US', Geeks);
console.log(dateTimeFormat3.format(dateformat));
Output:
"āĪŽāĨāΧāĪĩāĪūāΰ, 30 āĪāĨāĪēāĪūāĪ 1997"
"Wednesday, 30 July 1997"
"ŅŅÐĩÐīа, 30. ŅŅÐŧ 1997."
"Wednesday, July 30, 1997"
Example 2: In this example, we will print the specific dates, and days in three different languages.
javascript
let list = [new Date(2012, 08), new Date(2012, 11),
new Date(2012, 03)];
let geeks = { year: 'numeric', month: 'long' };
let dateTime = new Intl.DateTimeFormat('hi', geeks);
let result = list.map(dateTime.format);
console.log(result.join(' <-> '));
let dateTime1 = new Intl.DateTimeFormat('tr', geeks);
let result1 = list.map(dateTime1.format);
console.log(result1.join(' ; '));
let dateTime2 = new Intl.DateTimeFormat('LT', geeks);
let result2 = list.map(dateTime2.format);
console.log(result2.join(' :: '));
Output:
"āĪļāĪŋāĪĪāĪāĪŽāΰ 2012 <-> āĪĶāĪŋāĪļāĪāĪŽāΰ 2012 <-> āĪ
āĪŠāĨāΰāĨāĪē 2012"
"EylÞl 2012 ; AralÄąk 2012 ; Nisan 2012"
"2012 m. rugsÄjis :: 2012 m. gruodis :: 2012 m. balandis"
We have a complete list of Javascript Intl methods, to check those please go through the Javascript Intl Complete Reference article.
Supported Browsers: The browsers supported by Intl.DateTimeFormat.prototype.format() method are listed below:
JavaScript Intl DateTimeFormat format() Method – FAQs
How do you format dates in a specific timezone using Intl.DateTimeFormat?
Use the timeZone option within Intl.DateTimeFormat. For example, new Intl.DateTimeFormat(‘en-US’, { timeZone: ‘America/New_York’ }).format(new Date()) formats the date in the New York timezone.
Can Intl.DateTimeFormat handle different locales?
Yes, Intl.DateTimeFormat can format dates and times according to various locales by specifying a locale string, such as ‘en-US’ for U.S. English or ‘fr-FR’ for French.
What is the difference between Intl.DateTimeFormat and Date.toLocaleString()?
Both provide locale-sensitive formatting, but Intl.DateTimeFormat offers more customization and consistency across environments, while Date.toLocaleString() is a method on the Date object that provides a simpler interface for basic formatting.
How do you display the time in 12-hour or 24-hour format using Intl.DateTimeFormat?
Use the hour12 option. Set hour12: true for 12-hour format with AM/PM, and hour12: false for 24-hour format.
Can Intl.DateTimeFormat display the timezone name?
Yes, by using the timeZoneName option with values like ‘short’ or ‘long’, you can include the timezone name in the formatted output.
Similar Reads
JavaScript Intl DateTimeFormat format() Method
The Intl.DateTimeFormat format() method in JavaScript formats dates and times according to a specified locale and options. It allows you to customize the output with settings for timezone, date, time, and more, making it ideal for creating locale-sensitive and readable date-time strings. Syntax: Int
3 min read
JavaScript Intl DateTimeFormat formatRange() Method
The Intl.DateTimeFormat.prototype.formatRange() method is an inbuilt method in JavaScript that is used to format a date range in the most concise way based on the locale and options provided when instantiating Intl.DateTimeFormat object. Syntax: Intl.DateTimeFormat.prototype.formatRange(startDate, e
2 min read
JavaScript Intl DateTimeFormat formatToParts() Method
The Intl.DateTimeFormat.prototype.formatToParts() method is an inbuilt method in JavaScript which allows locale-aware formatting of strings produced by DateTimeFormat formatters. Syntax: dateTimeFormat.formatToParts( date ) Parameters: This method accepts a single parameter as mentioned above and de
2 min read
JavaScript Intl DateTimeFormat formatRangeToParts() Method
The Intl.DateTimeFormat.prototype.formatRangeToParts() method is an inbuilt method in JavaScript that is used to allow locale-specific tokens representing each part of the formatted date range produced by DateTimeFormat formatters. Syntax: Intl.DateTimeFormat.prototype.formatRangeToParts(startDate,
2 min read
JavaScript Intl DateTimeFormat resolvedOptions() Method
The Intl.DateTimeFormat.prototype.resolvedOptions() method is an inbuilt method in JavaScript that returns a new object with properties reflecting the locale, date, and time formatting options computed during the initialization of this DateTimeFormat object. Syntax: dateTimeFormat.resolvedOptions()
2 min read
JavaScript Intl DateTimeFormat supportedLocalesOf() Method
The Intl.DateTimeFormat.supportedLocalesOf() method is an inbuilt method in JavaScript that returns an array containing those of the provided locales that are supported in date and time formatting. Syntax: Intl.DateTimeFormat.supportedLocalesOf(locales, options) Parameters: This method accepts two p
2 min read
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:
2 min read
PHP | DateTime format() Function
The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format. Syntax: Object oriented style string DateTime::format( string $format ) or string DateTimeImmutable::format( string $format ) or string DateTimeInterface::f
1 min read
JavaScript Date toTimeString() Method
The date.toTimeString() method is used to return the time portion of the given date object in English. The date object is created using date() constructor. Syntax:dateObj.toTimeString()Parameters: This function does not accept any parameter. It is just used along with a Date object created using the
3 min read
JavaScript Date toDateString() Method
The date.toDateString() method converts the given date objectâs contents of the date portion into a string. The date object is created using the date() constructor. Syntax: dateObj.toDateString() Parameters: This method does not accept any parameter. It is just used with a Date object created using
4 min read