From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../date/tolocaledatestring/index.html | 155 +++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring') 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 +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

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

+ +
{{EmbedInteractiveExample("pages/js/date-tolocaledatestring.html")}}
+ + + +

语法

+ +
dateObj.toLocaleDateString([locales [, options]])
+ +

参数

+ +

 查看浏览器兼容性小节,看下哪些浏览器支持 locales 和 options 参数,还可以参看例子: 检测 localesoptions 参数支持情况

+ +

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

+ +

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

+ +

例子

+ +

例子:使用toLocaleDateString

+ +

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

+ +
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
+ +

例子:检测 localesoptions 参数支持情况

+ +

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

+ +
function toLocaleDateStringSupportsLocales() {
+    try {
+        new Date().toLocaleDateString("i");
+    } catch (e) {
+        return e​.name === "RangeError";
+    }
+    return false;
+}
+
+ +

例子:使用locales

+ +

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

+ +
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"
+
+ +

例子:使用options

+ +

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

+ +
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"
+
+ +

性能

+ +

当格式化大量日期时,最好创建一个 Intl.DateTimeFormat 对象,然后使用该对象 format 属性提供的方法。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 3rd Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', 'sec-15.9.5.6', 'Date.prototype.toLocaleDateString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.tolocaledatestring', 'Date.prototype.toLocaleDateString')}}{{Spec2('ES6')}} 
ECMAScript Internationalization API Specification, 1st EditionStandardDefines locales and options arguments.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.toLocaleDateString")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf