aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/intl/datetimeformat/index.html
blob: 72756680355e888f6e1f81701a72fd6dbb36f772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
---
title: Intl.DateTimeFormat
slug: Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
translation_of: Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
---
<div>{{JSRef}}</div>

<div><strong><code>Intl.DateTimeFormat</code></strong>是根据语言来格式化日期和时间的对象的构造器。</div>

<div>{{EmbedInteractiveExample("pages/js/intl-datetimeformat.html")}}</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><code>new Intl.DateTimeFormat([<var>locales</var>[, <var>options</var>]])
Intl.DateTimeFormat.call(<var>this</var>[, <var>locales</var>[, <var>options</var>]])</code></pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>locales</code></dt>
 <dd>
 <p>可选.缩写语言代码(BCP 47 language tag,例如:cmn-Hans-CN)的字符串或者这些字符串组成的数组. 关于参数locales的一般形式和解释请参见{{jsxref("Global_Objects/Intl", "Intl page", "#Locale_identification_and_negotiation", 1)}}. 下面的这些Unicode扩展键也是被允许的:</p>

 <p>译者注:下面这两种扩展的使用方式是language[-scripts][-region]-u-nu-* 和 language[-scripts][-region]-u-ca-* ,例如:zh-u-nu-hanidec(表示中文十进制数字) 和 zh-u-ca-chinese(表示中国日历,比如壬辰年冬月8日) ,也可以 nu 和 ca 组合使用 比如 使用 zh-u-ca-chinese-nu-hanidec 格式化Date.now()的返回值类似于"丙申年冬月九日"</p>

 <dl>
  <dt><code>nu</code></dt>
  <dd>编号系统. 可能的值包括: <code>"arab"</code>, <code>"arabext"</code>, <code>"bali"</code>, <code>"beng"</code>, <code>"deva"</code>, <code>"fullwide"</code>, <code>"gujr"</code>, <code>"guru"</code>, <code>"hanidec"</code>, <code>"khmr"</code>, <code>"knda"</code>, <code>"laoo"</code>, <code>"latn"</code>, <code>"limb"</code>, <code>"mlym"</code>, <code>"mong"</code>, <code>"mymr"</code>, <code>"orya"</code>, <code>"tamldec"</code>, <code>"telu"</code>, <code>"thai"</code>, <code>"tibt"</code>.</dd>
  <dt><code>ca</code></dt>
  <dd>日历. 可能的值包括: <code>"buddhist"</code>, <code>"chinese"</code>, <code>"coptic"</code>, <code>"ethioaa"</code>, <code>"ethiopic"</code>, <code>"gregory"</code>, <code>"hebrew"</code>, <code>"indian"</code>, <code>"islamic"</code>, <code>"islamicc"</code>, <code>"iso8601"</code>, <code>"japanese"</code>, <code>"persian"</code>, <code>"roc"</code>.</dd>
 </dl>
 </dd>
 <dt><code>options</code></dt>
 <dd>
 <p>可选. 包含一些或所有的下面属性的类:</p>

 <dl>
  <dt><code>localeMatcher</code></dt>
  <dd>使用的local的匹配算法. 可能的值有"lookup"和"best fit"; 默认值是 <code>"best fit"</code>. 有关此选项的信息, 请参见{{jsxref("Global_Objects/Intl", "Intl page", "#Locale_negotiation", 1)}}.</dd>
  <dt><code>timeZone</code></dt>
  <dd>使用的时区. 这唯一的值实现必须被标准世界时间(UTC)所识别。默认值是运行时的默认时区. <a href="https://www.iana.org/time-zones">IANA time zone database</a>中的时区名称可能会被识别, 例如<code>"Asia/Shanghai"</code>, <code>"Asia/Kolkata"</code>, <code>"America/New_York"</code>.</dd>
  <dt><code>hour12</code></dt>
  <dd>是否使用12小时时间制(而不是24小时的时间). 可能的值是<code>true</code> 或 <code>false</code>; 默认值是根据locale来自动决定的(译者注:中国地区的默认值为true).</dd>
  <dt><code>formatMatcher</code></dt>
  <dd><code>format的匹配算法.可能的值有"basic"和"best fit";默认值是"best fit".有关此属性使用的信息,参见以下段落。</code>日期时间插件被格式化输出时可以使用的属性集合描述。实现需要支持是以下子集中的其中一个(译者注:当weekday,year等这些属性一个也不使用的时候,在cmn-Hans-CN中相当于使用集合year, month, day, hour, minute, second并且它们的值都是numeric):</dd>
 </dl>

 <ul>
  <li><code>weekday</code>, <code>year</code>, <code>month</code>, <code>day</code>, <code>hour</code>, <code>minute</code>, <code>second</code></li>
  <li><code>weekday</code>, <code>year</code>, <code>month</code>, <code>day</code></li>
  <li><code>year</code>, <code>month</code>, <code>day</code></li>
  <li><code>year</code>, <code>month</code></li>
  <li><code>month</code>, <code>day</code></li>
  <li><code>hour</code>, <code>minute</code>, <code>second</code></li>
  <li><code>hour</code>, <code>minute</code></li>
 </ul>

 <p>实现可能支持其他的子集,并通过对所有可用的子集对比找到最匹配的子集。通过 formatMatcher属性可以设置两种算法用于对比和选择子集:  <a href="http://www.ecma-international.org/ecma-402/1.0/#BasicFormatMatcher">完全匹配<code>"basic"算法</code> </a> 和 一种依赖于“best fit”算法的实现.</p>

 <dl>
  <dt><code>weekday</code></dt>
  <dd>工作日的展现方式.可能的值有 <code>"narrow"</code>, <code>"short"</code>, <code>"long"</code>.</dd>
  <dt><code>era</code></dt>
  <dd>纪元的展现方式. 可能的值有 <code>"narrow"</code>, <code>"short"</code>, <code>"long"</code>.</dd>
  <dt><code>year</code></dt>
  <dd>年的展现方式.  可能的值有 <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
  <dt><code>month</code></dt>
  <dd>月的展现方式. 可能的值有 <code>"numeric"</code>, <code>"2-digit"</code>, <code>"narrow"</code>, <code>"short"</code>, <code>"long"</code>.</dd>
  <dt><code>day</code></dt>
  <dd>日的展现方式.可能的值有 <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
  <dt><code>hour</code></dt>
  <dd>时的展现方式.可能的值有 <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
  <dt><code>minute</code></dt>
  <dd>分钟的展现方式.可能的值有 <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
  <dt><code>second</code></dt>
  <dd>秒的展现方式. 可能的值有<code>"numeric"</code>, <code>"2-digit"</code>.</dd>
  <dt><code>timeZoneName</code></dt>
  <dd>时区名称的展现方式.可能的值有 <code>"short"</code>, <code>"long"</code>.</dd>
 </dl>

 <p>每个日期时间组件属性的默认值都是{{jsxref("undefined")}},但是若所有的组件属性都是{{jsxref("undefined")}},那么<code>year</code>, <code>month</code>, and <code>day</code> 的值就都被认为是"<code>numeric</code>".</p>
 </dd>
</dl>

<h2 id="描述">描述</h2>

<h3 id="属性">属性</h3>

<dl>
 <dt>{{jsxref("DateTimeFormat.prototype", "Intl.DateTimeFormat.prototype")}}</dt>
 <dd>允许将属性添加到所有对象上。</dd>
</dl>

<h3 id="方法">方法</h3>

<dl>
 <dt>{{jsxref("DateTimeFormat.supportedLocalesOf", "Intl.DateTimeFormat.supportedLocalesOf()")}}</dt>
 <dd>Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.</dd>
</dl>

<h2 id="DateTimeFormat_实例"><code>DateTimeFormat</code> 实例</h2>

<h3 id="属性_2">属性</h3>

<p><code>DateTimeFormat实例集成以下原型的属性</code>:</p>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/prototype', 'Properties')}}</div>

<h3 id="方法_2">方法</h3>

<p><code>DateTimeFormat实例集成以下原型的方法</code>:</p>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/prototype', 'Methods')}}</div>

<h2 id="实例">实例</h2>

<h3 id="使用_DateTimeFormat">使用 <code>DateTimeFormat</code></h3>

<p>不指定locale时,DateTimeFormat使用默认的locale 和 默认的 options</p>

<pre class="brush: js">var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

//参数未填时使用默认的locale和默认的时区
console.log(new Intl.DateTimeFormat().format(date));
//如果是在洛杉矶那么值为 → "12/19/2012"
</pre>

<h3 id="使用locales">使用<code>locales</code></h3>

<p>这个例子显示了本地化日期和时间格式的一些变化。为了获得用于您的应用程序的用户界面的语言格式,请确保使用locales参数确保指定语言(可能还有一些回退语言):</p>

<pre class="brush: js">var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// 下面是假定的所在时区
// 洛杉矶(America/Los_Angeles for the US)

// 美式英语(US English) 使用  month-day-year 格式
console.log(new Intl.DateTimeFormat('en-US').format(date));
// → "12/19/2012"

// 英式英语(British English) 使用 day-month-year 格式
console.log(new Intl.DateTimeFormat('en-GB').format(date));
// → "20/12/2012"

// 韩国使用 year-month-day 格式
console.log(new Intl.DateTimeFormat('ko-KR').format(date));
// → "2012. 12. 20."

//大部分阿拉伯国家使用阿拉伯字母(real Arabic digits)
console.log(new Intl.DateTimeFormat('ar-EG').format(date));
// → "٢٠‏/١٢‏/٢٠١٢"

//在日本,应用可能想要使用日本日历,
//2012 是平成24年(平成是是日本天皇明仁的年号,由1989年1月8日起开始计算直至现在)
console.log(new Intl.DateTimeFormat('ja-JP-u-ca-japanese').format(date));
// → "平成24/12/20"

//当请求一个语言可能不支持,如巴厘(ban),若有备用的语言印尼语(id),
//那么将使用印尼语(id)
console.log(new Intl.DateTimeFormat(['ban', 'id']).format(date));
// → "20/12/2012"
</pre>

<h3 id="使用options">使用<code>options</code></h3>

<p>可以使用 <code>options </code>参数来自定义 日期时间格式化方法返回的字符串。</p>

<pre class="brush: js">var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

//请求参数(options)中包含参数星期(weekday),并且该参数的值为长类型(long)
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(new Intl.DateTimeFormat('de-DE', options).format(date));
// → "Donnerstag, 20. Dezember 2012"

// 一个应用使用 世界标准时间(UTC),并且UTC使用短名字(short)展示
options.timeZone = 'UTC';
options.timeZoneName = 'short';//若不写这一行那么仍然显示的是世界标准时间;但是GMT三个字母不会显示
console.log(new Intl.DateTimeFormat('en-US', options).format(date));
// → "Thursday, December 20, 2012, GMT"

// 有时你想变得更精确
options = {
  hour: 'numeric', minute: 'numeric', second: 'numeric',
  timeZoneName: 'short'
};
console.log(new Intl.DateTimeFormat('en-AU', options).format(date));
// → "2:00:00 pm AEDT"

// 使用24小时制
options = {
  year: 'numeric', month: 'numeric', day: 'numeric',
  hour: 'numeric', minute: 'numeric', second: 'numeric',
  hour12: false
};
console.log(date.toLocaleString('en-US', options));
// → "12/19/2012, 19:00:00"
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范版本</th>
   <th scope="col">规范状态</th>
   <th scope="col">注解</th>
  </tr>
  <tr>
   <td>{{SpecName('ES Int 1.0', '#sec-12.1', 'Intl.DateTimeFormat')}}</td>
   <td>{{Spec2('ES Int 1.0')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES Int 2.0', '#sec-12.1', 'Intl.DateTimeFormat')}}</td>
   <td>{{Spec2('ES Int 2.0')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES Int Draft', '#datetimeformat-objects', 'Intl.DateTimeFormat')}}</td>
   <td>{{Spec2('ES Int Draft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat}}

<h2 id="相关链接">相关链接</h2>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl', 'See_also')}}</div>