diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html new file mode 100644 index 0000000000..b5dfe22d12 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html @@ -0,0 +1,155 @@ +--- +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" name="Syntax">语法</h2> + +<pre class="syntaxbox"><var>dateObj</var>.toLocaleDateString([locales [, options]])</pre> + +<h3 id="Parameters" name="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" name="Examples">例子</h2> + +<h3 id="Example:_Using_toLocaleDateString" name="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" name="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" name="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")); +// → "<span dir="rtl">٢٠/١٢/٢٠١٢</span>" + +// 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" name="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" name="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<sup>st</sup> Edition</a></td> + <td>Standard</td> + <td>Defines <code>locales</code> and <code>options</code> arguments.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.Date.toLocaleDateString")}}</p> + +<h2 id="See_Also" name="See_Also">相关链接</h2> + +<ul> + <li>{{jsxref("Global_Objects/DateTimeFormat", "DateTimeFormat")}}</li> + <li>{{jsxref("Date.prototype.toLocaleString()")}}</li> + <li>{{jsxref("Date.prototype.toLocaleTimeString()")}}</li> +</ul> |