1. 相对时间国际化
支持 "second", "minute", "hour", "day", "week", "month", "quarter", "year"
var rtf = new Intl.RelativeTimeFormat('zh-Hans-CN',{numeric: "auto"});
rtf.format(-1, "day");
//"昨天"
rtf.format(-2, "day");
//"前天"
var rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
rtf.format(-1, "day");
// "yesterday"
rtf.format(1, "day");
//"tomorrow"
2. 数字国际化
2.1 分组逗号分隔
(1234.2345).toLocaleString('en-US',{useGrouping:true});
//"1,234.235"
2.2 科学计数保留位
(1234.2345).toLocaleString('zh-CN', {style:'decimal',maximumSignificantDigits:2});
//"1,200"
// 德语使用逗号作为小数点,使用.作为千位分隔符
new Intl.NumberFormat('de-DE').format(number)
// → 123.456,789
(1234567890).toLocaleString('zh-Hans-CN-u-nu-hanidec',{useGrouping:false})
//"一二三四五六七八九〇"
(123456.0199).toLocaleString('zh-Hans-CN-u-nu-hanidec')
//"一二三,四五六.〇二"
2.3 小数保留位
(1234.2345).toLocaleString('zh-CN', { style: 'decimal',maximumFractionDigits:3});
//"1,234.235"
2.4 百分制
(1234.2345).toLocaleString('zh-CN', { style: 'percent',maximumFractionDigits:2});
//"123,423.45%"
2.5 货币
(1234.23).toLocaleString('zh-CN', { style: 'currency',currency:"CNY",
currencyDisplay:"symbol",
maximumFractionDigits:2});
//"¥1,234.23"
(1234.23).toLocaleString('zh-CN', { style: 'currency',currency:"CNY",
currencyDisplay:"code",
maximumFractionDigits:2});
//"CNY 1,234.23"
(1234.23).toLocaleString('zh-CN', { style: 'currency',currency:"CNY",
currencyDisplay:"name",
maximumFractionDigits:2});
//"1,234.23 人民币"
var number = 123456.789;
// 请求一个货币格式
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// → 123.456,79 €
// the Japanese yen doesn't use a minor unit
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// → ¥123,457
// 只显示三个有效数字
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));
// → 1,23,000
3 日期时间国际化
var date=new Date(Date.UTC(2012, 11, 20, 3, 0, 0))
Intl.DateTimeFormat('en-US').format(date)//"12/20/2012"
Intl.DateTimeFormat('en-GB').format(date)//"20/12/2012"
Intl.DateTimeFormat('ko-KR').format(date)//"2012. 12. 19."
Intl.DateTimeFormat('ar-EG').format(date)//"١٩/١٢/٢٠١٢"
3.1 时间国际化
date.toLocaleTimeString('zh-Hans-CN')
//"上午11:00:00"
3.2 日期国际化
date.toLocaleDateString('zh-Hans-CN')
"2012/12/20"
星期文案转换
(new Date('2001-01-01')).toLocaleString('zh-Hans-CN',{weekday:"long"})
//"星期一"
(new Date('2001-01-07')).toLocaleString('zh-Hans-CN',{weekday:"short"})
//"周日"
Date.prototype.toLocaleString 配置项
date.toLocaleString('zh-Hans-CN',{
year:"numeric",
month:"2-digit",
day:"2-digit",
weekday:"long",
hour:"2-digit",
minute:"2-digit",
second:"2-digit",
hour12: false,
timeZone:"Asia/Shanghai",
})
//"2012年12月20日星期四 11:00:00"
Intl格式化(含时区转换)
Intl.DateTimeFormat('zh-Hans-CN',{
year:"numeric",
month:"2-digit",
day:"2-digit",
hour:"2-digit",
minute:"2-digit",
second:"2-digit",
hour12: false,
timeZone:"Asia/Shanghai"
}).format(date);
//"2012/12/20 11:00:00"
Intl格式化分片
Intl.DateTimeFormat('zh-Hans-CN',{
year:"numeric",
month:"2-digit",
day:"2-digit",
hour:"2-digit",
minute:"2-digit",
second:"2-digit",
hour12: false,
timeZone:"Asia/Shanghai",
}).formatToParts(date)
/*
0: {type: "year", value: "2012"}
1: {type: "literal", value: "/"}
2: {type: "month", value: "12"}
3: {type: "literal", value: "/"}
4: {type: "day", value: "12"}
5: {type: "literal", value: " "}
6: {type: "hour", value: "11"}
7: {type: "literal", value: ":"}
8: {type: "minute", value: "00"}
9: {type: "literal", value: ":"}
10: {type: "second", value: "00"}
*/
农历阳历转换
Intl.DateTimeFormat('zh-Hans-CN-u-ca-chinese').format(date);
//"29/11/8"
//备注:农历壬辰年 十一月初八
zhMon=[' 甲乙丙丁戊己庚辛壬癸',' 子丑寅卯辰巳午未申酉戌亥']
zhMon[0][29%10]+zhMon[1][29%12]//壬辰
date.toLocaleString('zh-Hans-CN-u-ca-chinese')
//"29/11/8 上午11:00:00"
4. 敏感字符比较
Intl.Collator
// 德语中, ä 使用 a 作为基本字母
new Intl.Collator('de', { sensitivity: 'base' }).compare('ä', 'a')
// → 0
5. 集合描述国际化
Intl.ListFormat
const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
formatter.format(vehicles)
// expected output: "Motorcycle, Bus, and Car"
const formatter2 = new Intl.ListFormat('de', { style: 'short', type: 'disjunction' });
formatter2.format(vehicles)
// expected output: "Motorcycle, Bus oder Car"
const formatter3 = new Intl.ListFormat('en', { style: 'narrow', type: 'unit' });
formatter3.format(vehicles)
// expected output: "Motorcycle Bus Car"
6. 复数描述国际化
Intl.PluralRules
var pr=new Intl.PluralRules('ar-EG')
pr.select(0);
// → 'zero'
pr.select(1);
// → 'one'
pr.select(2);
// → 'two'
pr.select(6);
// → 'few'
pr.select(18);
// → 'many'
var pr = new Intl.PluralRules('en-US', { type: 'ordinal' });
pr.select(0);
// → 'other'
pr.select(1);
// → 'one'
pr.select(2);
// → 'two'
pr.select(3);
// → 'few'
pr.select(4);
// → 'other'
pr.select(42);
// → 'two'
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。