Intl 對象是 ECMAScript 國際化 API 的一個(gè)命名空間,它提供了精確的字符串對比(Collator ),數(shù)字格式化(NumberFormat),日期和時(shí)間格式化(DateTimeFormat)。
Intl.Collator 是用于語言敏感字符串比較的 collators構(gòu)造函數(shù)。
詳細(xì)參考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Collator
Intl.DateTimeFormat是根據(jù)語言來格式化日期和時(shí)間的類的構(gòu)造器類
參考鏈接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
實(shí)例:
var date = new Date();
//參數(shù)未填時(shí)使用默認(rèn)的locale和默認(rèn)的時(shí)區(qū)
console.log(new Intl.DateTimeFormat().format(date));//2017/01/01
Intl.NumberFormat是對語言敏感的格式化數(shù)字類的構(gòu)造器類
詳細(xì)參考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
var number = 123456.00;
// 通過編號系統(tǒng)中的nu擴(kuò)展鍵請求, 例如中文十進(jìn)制數(shù)字
console.log(new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec').format(2));//二
// 請求一個(gè)貨幣格式
console.log(new Intl.NumberFormat('zh-Hans-CN',
{ style: 'currency', currency: 'CNY'}).format(number));//¥123,456.00