JavaScript's built-in internationalization library.
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
new Intl.DateTimeFormat('en-US').format(date);
// "12/20/2012"
new Intl.DateTimeFormat('en-GB').format(date);
// "20/12/2012"
new Intl.DateTimeFormat('ja-JP').format(date);
// "2012/12/20"
Array#toLocaleString
(#615)// Create a segmenter in your locale
let segmenter = new Intl.Segmenter("en", {granularity: "word"});
// Iterate over a string!
for (let {
index,
segment,
isWordLike
} of segmenter.segment("hello world")) {
console.log((isWord?"Word":"Non-word")+" '"+segment+"' @ "+index);
}
// => "Word 'hello' @ 0", "Non-word ' ' @ 5", โฆ
const jaJP = new Intl.Locale("ja-JP")
jaJP.calendars // ["gregory", "japanese"]
jaJP.collations // ["unihan", "emoji", "eor"]
jaJP.hourCycles // ["h23"]
jaJP.numberingSystems // ["latn"]
jaJP.timeZones // ["Asia/Tokyo"]}
const enUS = new Intl.Locale("en-US")
enUS.calendars // ["gregory"]
enUS.collations // ["emoji", "eor"]
enUS.hourCycles // ["h12"]
enUS.numberingSystems // ["latn"]
enUS.timeZones // ["America/Adak", "America/Anchorage", "America/Boise", "America/Chicago", "America/Denver", "America/Detroit", "America/Indiana/Knox", "America/Indiana/Marengo", "America/Indiana/Petersburg", "America/Indiana/Tell_City", "America/Indiana/Vevay", "America/Indiana/Vincennes", "America/Indiana/Winamac", "America/Indianapolis", "America/Juneau", "America/Kentucky/Monticello", "America/Los_Angeles", "America/Louisville", "America/Menominee", "America/Metlakatla", "America/New_York", "America/Nome", "America/North_Dakota/Beulah", "America/North_Dakota/Center", "America/North_Dakota/New_Salem", "America/Phoenix", "America/Sitka", "America/Yakutat", "Pacific/Honolulu"]
dn = new Intl.DisplayNames("en", {type: "calendar"})
dn.of("roc") // "Minguo Calendar"
dn.of("persian") // "Persian Calendar"
dn.of("gregory") // "Gregorian Calendar"
dn.of("ethioaa") // "Ethiopic Amete Alem Calendar"
dn.of("japanese") // "Japanese Calendar"
dn.of("dangi") // "Dangi Calendar"
dn.of("chinese") // "Chinese Calendar"
dn = new Intl.DisplayNames("ja", {type: "calendar"})
dn.of("roc") // "ไธญ่ฏๆฐๅฝๆฆ"
dn.of("persian") // "ใใซใทใขๆฆ"
dn.of("gregory") // "่ฅฟๆฆ(ใฐใฌใดใชใชๆฆ)"
dn.of("ethioaa") // "ใจใใชใใขๅตไธ็ดๅ
ๆฆ"
dn.of("japanese") // "ๅๆฆ"
dn.of("dangi") // "ใใณใฎๆฆ"
dn.of("chinese") // "ไธญๅฝๆฆ"
let timeZoneNames = ["short", "long", "shortOffset", "longOffset", "shortGeneric", "longGeneric"];
timeZoneNames.forEach(function(timeZoneName) {
print(timeZoneName + ": " + (new Date()).toLocaleTimeString("en", {timeZoneName}))
});
// short: 12:27:10 PM PST
// long: 12:27:10 PM Pacific Standard Time
// shortOffset: 12:27:10 PM GMT-8
// longOffset: 12:27:10 PM GMT-08:00
// shortGeneric: 12:27:10 PM PT
// longGeneric: 12:27:10 PM Pacific Time
timeZoneNames.forEach(function(timeZoneName) {
print(timeZoneName + ": " + (new Date()).toLocaleTimeString("ja", {timeZoneName}))
});
// short: 1:28:08 GMT+5:30
// long: 1ๆ28ๅ08็ง ใคใณใๆจๆบๆ
// shortOffset: 1:28:08 GMT+5:30
// longOffset: 1:28:08 GMT+05:30
// shortGeneric: 1:28:08 ใคใณใๆ้
// longGeneric: 1:28:08 ใคใณใๆจๆบๆ
// Find out the supported calendars
Intl.supportedValuesOf("calendar").forEach(function(calendar) {
// 'buddhist', 'chinese', ... 'islamicc'
});
// Find out the supported currencies
Intl.supportedValuesOf("currency").forEach(function(currency) {
// 'AED', 'AFN', 'ALL', ... 'ZWL'
});
// Find out the supported time zones
Intl.supportedValuesOf("timeZone").forEach(function(timeZone) {
// 'Africa/Abidjan', 'Africa/Accra', ... 'Pacific/Wallis'
});
Intl.NumberFormat
v3const nf = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "EUR",
maximumFractionDigits: 0,
});
nf.formatRange(2.9, 3.1); // "~โฌ3"
const nf = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "EUR",
signDisplay: "always",
});
nf.formatRange(2.999, 3.001); // "~+โฌ3.00"
Intl.DurationFormat
(Stage 2, USA)
Smart Unit Preferences (Stage 1, YMD)
Intl.DateTimeFormat
eraDisplay (Stage 1, LAF)
Intl
LocaleMatcher (Stage 1, LHO)