aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html
blob: d6c8e7e58e93db917e75455d992fe46400b05f07 (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
---
title: Date.prototype.toLocaleDateString()
slug: Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
translation_of: Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
---
<div>{{JSRef("Global_Objects", "Date")}}</div>

<p><code><strong>toLocaleDateString()</strong></code> 方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同。新增的参数 <code>locales</code> 和 <code>options</code> 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中, <code>locales</code><code>options</code> 参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。</p>

<div>{{EmbedInteractiveExample("pages/js/date-tolocaledatestring.html")}}</div>



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

<pre class="syntaxbox"><var>dateObj</var>.toLocaleDateString([locales [, options]])</pre>

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

<p> 查看<a href="#Browser_Compatibility" title="#Browser_Compatibility">浏览器兼容性</a>小节,看下哪些浏览器支持 <code>locales</code> 和 <code>options</code> 参数,还可以参看<a href="#Example:_Checking_for_support_for_locales_and_options_arguments">例子: 检测 <code>locales</code><code>options</code> 参数支持情况</a></p>

<p>{{page('zh-CN/docs/JavaScript/Reference/Global_Objects/DateTimeFormat','Parameters')}}</p>

<p>The default value for each date-time component property is <code>undefined</code>, but if the <code>weekday</code>, <code>year</code>, <code>month</code>, <code>day</code> properties are all <code>undefined</code>, then <code>year</code>, <code>month</code>, and <code>day</code> are assumed to be "numeric".</p>

<h2 id="Examples">例子</h2>

<h3 id="Example:_Using_toLocaleDateString">例子:使用<code>toLocaleDateString</code></h3>

<p>没有指定语言环境(locale)时,返回一个使用默认语言环境和格式设置(options)的格式化字符串。</p>

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

// toLocaleDateString without arguments depends on the implementation,
// the default locale, and the default time zone
date.toLocaleDateString();
// → "12/11/2012" if run in en-US locale with time zone America/Los_Angeles</pre>

<h3 id="Example:_Checking_for_support_for_locales_and_options_arguments">例子:检测 <code>locales</code><code>options</code> 参数支持情况</h3>

<p><code>locales</code> 和 <code>options</code> 参数不是所有的浏览器都支持。为了检测一种实现环境(implementation)是否支持它们,可以使用不合法的语言标签,如果实现环境支持该参数,则会抛出一个 <code>RangeError</code> 异常,反之会忽略参数。</p>

<pre class="brush: js">function toLocaleDateStringSupportsLocales() {
    try {
        new Date().toLocaleDateString("i");
    } catch (e) {
        return e​.name === "RangeError";
    }
    return false;
}
</pre>

<h3 id="Example:_Using_locales">例子:使用<code>locales</code></h3>

<p>下例展示了本地化日期格式的一些变化。为了在应用的用户界面得到某种语言的日期格式,必须确保使用 <code>locales</code> 参数指定了该语言(可能还需要设置某些回退语言)。</p>

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

// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US

// US English uses month-day-year order
alert(date.toLocaleDateString("en-US"));
// → "12/19/2012"

// British English uses day-month-year order
alert(date.toLocaleDateString("en-GB"));
// → "20/12/2012"

// Korean uses year-month-day order
alert(date.toLocaleDateString("ko-KR"));
// → "2012. 12. 20."

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(date.toLocaleDateString("ar-EG"));
// → "٢٠‏/١٢‏/٢٠١٢"

// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
alert(date.toLocaleDateString("ja-JP-u-ca-japanese"));
// → "24/12/20"

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(date.toLocaleDateString(["ban", "id"]));
// → "20/12/2012"
</pre>

<h3 id="Example:_Using_options">例子:使用<code>options</code></h3>

<p>可以使用 <code>options </code>参数来自定义 <code>toLocaleDateString</code> 方法返回的字符串。</p>

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

// request a weekday along with a long date
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
alert(date.toLocaleDateString("de-DE", options));
// → "Donnerstag, 20. Dezember 2012"

// an application may want to use UTC and make that visible
options.timeZone = "UTC";
options.timeZoneName = "short";
alert(date.toLocaleDateString("en-US", options));
// → "Thursday, December 20, 2012, GMT"
</pre>

<h2 id="Performance">性能</h2>

<p>当格式化大量日期时,最好创建一个 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat" title="/en-US/docs/JavaScript/Reference/Global_Objects/DateTimeFormat"><code>Intl.DateTimeFormat</code></a> 对象,然后使用该对象 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/format" title="/en-US/docs/JavaScript/Reference/Global_Objects/DateTimeFormat/format"><code>format</code></a> 属性提供的方法。</p>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范版本</th>
   <th scope="col">规范状态</th>
   <th scope="col">注解</th>
  </tr>
  <tr>
   <td>ECMAScript 3rd Edition. Implemented in JavaScript 1.0</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', 'sec-15.9.5.6', 'Date.prototype.toLocaleDateString')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-date.prototype.tolocaledatestring', 'Date.prototype.toLocaleDateString')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td><a href="http://www.ecma-international.org/ecma-402/1.0/#sec-13.3.2">ECMAScript Internationalization API Specification, 1 Edition</a></td>
   <td>Standard</td>
   <td>Defines <code>locales</code> and <code>options</code> arguments.</td>
  </tr>
 </tbody>
</table>

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

<p>{{Compat("javascript.builtins.Date.toLocaleDateString")}}</p>

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

<ul>
 <li>{{jsxref("Global_Objects/DateTimeFormat", "DateTimeFormat")}}</li>
 <li>{{jsxref("Date.prototype.toLocaleString()")}}</li>
 <li>{{jsxref("Date.prototype.toLocaleTimeString()")}}</li>
</ul>