举例 创建DateTimeFormat对象后,下一步是通过方便的format()函数来格式化date。更方便的是,这个函数是有界函数(bound function):你不必在DateTimeFormat上直接调用。之后给它传递一个时间戳或者Date对象。
总结一下,下文是如何为特定用途创建DateTimeFormat选项的例子(在当前Firefox执行行为下)。 1 2 3 4 | var msPerDay = 24 * 60 * 60 * 1000;
var july172014 = new Date(msPerDay * (44 * 365 + 11 + 197));
|
我们来格式化美国英语的date。我们先创建一个2位数字的month/day/year, 加上2位数字的hours/minutes, 还有一个短时区来确定这个时间。(结果肯定因不同时区而明显不同) 1 2 3 4 5 6 7 8 | 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;
print(americanDateTime(july172014));
|
或者类似的,对葡萄牙语,最好是巴西使用的葡语,但是用在葡萄牙作品中的。格式会稍长,因为包含完整的year和正式拼写的month,但是因移植性要转成UTC格式。 1 2 3 4 5 6 7 8 9 | var options =
{ year: "numeric" , month: "long" , day: "numeric" ,
hour: "2-digit" , minute: "2-digit" ,
timeZoneName: "short" , timeZone: "UTC" };
var portugueseTime =
new Intl.DateTimeFormat([ "pt-BR" , "pt-PT" ], options);
print(portugueseTime.format(july172014));
|
那对于一个压缩的,UTC格式的瑞士火车每周调度表?我们尝试用正式语言按流行度从大到小来选择最易读的一个。 1 2 3 4 5 6 7 8 | var swissLocales = [ "de-CH" , "fr-CH" , "it-CH" , "rm-CH" ]; var options =
{ weekday: "short" ,
hour: "numeric" , minute: "numeric" ,
timeZone: "UTC" , timeZoneName: "short" };
var swissTime =
new Intl.DateTimeFormat(swissLocales, options).format;
print(swissTime(july172014));
|
或者我们尝试某个日本博物馆里的一幅画中的描述性文本中的date,这个date使用日本的year和era历法。 1 2 3 4 5 | var jpYearEra =
new Intl.DateTimeFormat( "ja-JP-u-ca-japanese" ,
{ year: "numeric" , era: "long" });
print(jpYearEra.format(july172014));
|
对一些完全不同的、更长的date,用于泰国泰语,但是使用泰语数字系统和中国历法。(类似Firefox的高质量实现通常会将普通的th-TH当做th-TH-u-ca-buddhist-nu-latn, 因为泰国使用佛历系统和拉丁0-9数字)。 1 2 3 4 5 6 | var options =
{ year: "numeric" , month: "long" , day: "numeric" };
var thaiDate =
new Intl.DateTimeFormat( "th-TH-u-nu-thai-ca-chinese" , options);
print(thaiDate.format(july172014));
|
撇开历法和数字系统,还是很简单的。只要选取自己的组件和长度。
|