Collator, NumberFormat, 和 DateTimeFormat 对象的构造函数是Intl对象的属性. Link to section日期和时间

日期和时间格式化
DateTimeFormat 对象在日期和时间的格式化方面很有用. 下面的代码把一个日期格式化为美式英语格式. (不同时区结果不同.)

var msPerDay = 24 * 60 * 60 * 1000;

// July 17, 2014 00:00:00 UTC.
var july172014 = new Date(msPerDay * (44 * 365 + 11 + 197));//2014-1970=44年
//这样创建日期真是醉人。。。还要自己计算天数。。。11是闰年中多出的天数。。。
//197是6×30+16(7月的16天)+3(3个大月)-2(2月少2天)

var options = { year: “2-digit”, month: “2-digit”, day: “2-digit”,
hour: “2-digit”, minute: “2-digit”, timeZoneName: “short” };
var americanDateTime = new Intl.DateTimeFormat(“en-US”, options).format;

console.log(americanDateTime(july172014)); // 07/16/14, 5:00 PM PDT
Link to section数字格式化
NumberFormat 对象在数字的格式化方面很有用, 比如货币数量值.

var gasPrice = new Intl.NumberFormat(“en-US”,
{ style: “currency”, currency: “USD”,
minimumFractionDigits: 3 });

console.log(gasPrice.format(5.259)); // $5.259

var hanDecimalRMBInChina = new Intl.NumberFormat(“zh-CN-u-nu-hanidec”,
{ style: “currency”, currency: “CNY” });

console.log(hanDecimalRMBInChina.format(1314.25)); // ¥ 一,三一四.二五
Link to section定序
Collator 对象在字符串比较和排序方面很有用.

举例, 德语中有两种不同的排序方式 电话本(phonebook) 和 字典(dictionary). 电话本排序强调发音, 比如在排序前 “ä”, “ö”等被扩展为 “ae”, “oe”等发音.

var names = [“Hochberg”, “Hönigswald”, “Holzman”];

var germanPhonebook = new Intl.Collator(“de-DE-u-co-phonebk”);

// as if sorting [“Hochberg”, “Hoenigswald”, “Holzman”]:
console.log(names.sort(germanPhonebook.compare).join(“, “));
// logs “Hochberg, Hönigswald, Holzman”
有些德语词包含变音, 所以在字典中忽略变音进行排序是合理的 (除非待排序的单词只有变音部分不同: schon 先于 schön).

var germanDictionary = new Intl.Collator(“de-DE-u-co-dict”);

// as if sorting [“Hochberg”, “Honigswald”, “Holzman”]:
console.log(names.sort(germanDictionary.compare).join(“, “));
// logs “Hochberg, Holzman, Hönigswald”重点内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值