JavaScript Intl DateTimeFormat supportedLocalesOf() Method

JavaScript Intl DateTimeFormat supportedLocalesOf() Method is used to return an array containing those of the provided locales that are supported in date and time formatting. This method can be useful for adapting your application’s date and time formatting based on the available locales.

Syntax

Intl.DateTimeFormat.supportedLocalesOf(locales, options)

Parameters

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

options:  It is an object that has localeMatcher property.

  • localeMatcher: The locale matching algorithm to use. It has two values: lookup and best fit(default).

Return Value

It returns an array of strings representing the subset of the given locale tags that are supported in date and time formatting. If none are supported, it returns an empty array.

Example 1: How to Use JavaScript Intl DateTimeFormat supportedLocalesOf() Method

let locales = ['en-US', 'es-ES', 'fr-FR', 'ja-JP', 'lingston'];

let supportedLocales = Intl.DateTimeFormat.supportedLocalesOf(locales);

console.log(supportedLocales);

Output

[ 'en-US', 'es-ES', 'fr-FR', 'ja-JP' ]

In this example, lingston is likely not a supported locale, so it is not included in the output array.

Example 2: Using localeMatcher option

let locales = ['en-US','ban', 'id-u-co-pinyin', 'de-ID', 'fr', 'zh-Hans-CN'];

let supportedLocalesOf = { localeMatcher: 'lookup' };

console.log(Intl.DateTimeFormat.supportedLocalesOf(locales, supportedLocalesOf));

Output

[ 'en-US', 'id-u-co-pinyin', 'de-ID', 'fr', 'zh-Hans-CN' ]

Browser Compatibility

Chrome 24+ Edge 12+ Firefox 29+ Safari 10+ Opera 15+
Yes Yes Yes Yes Yes

That’s it!

Related posts

JavaScript Int.DateTimeFormat() constructor

JavaScript Intl.DateTimeFormat.format() Method

JavaScript Intl DateTimeFormat formatRange() Method

JavaScript Intl DateTimeFormat formatRangeToParts() Method

Leave a Comment