From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/date/toisostring/index.html | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/toisostring/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/date/toisostring') diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/toisostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/toisostring/index.html new file mode 100644 index 0000000000..b93bd8c745 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/toisostring/index.html @@ -0,0 +1,89 @@ +--- +title: Date.prototype.toISOString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toISOString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toISOString +--- +
{{JSRef}}
+ +

toISOString() 方法返回一个 ISO(ISO 8601 Extended Format)格式的字符串: YYYY-MM-DDTHH:mm:ss.sssZ。时区总是UTC(协调世界时),加一个后缀“Z”标识。

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

语法

+ +
dateObj.toISOString()
+ +

例子

+ +
var today = new Date("05 October 2011 14:48 UTC");
+alert(today.toISOString()); // 返回2011-10-05T14:48:00.000Z
+
+ +

上例使用了非标准字符串的解析,该字符串在某些旧的浏览器(如IE)中可能无法被正确解析。

+ +

Polyfill

+ +

该方法在ECMA-262第5版中被标准化。对于那些不支持此方法的JS引擎可以通过加上下面的代码实现:

+ +
if ( !Date.prototype.toISOString ) {
+  ( function() {
+
+    function pad(number) {
+      if ( number < 10 ) {
+        return '0' + number;
+      }
+      return number;
+    }
+
+    Date.prototype.toISOString = function() {
+      return this.getUTCFullYear() +
+        '-' + pad( this.getUTCMonth() + 1 ) +
+        '-' + pad( this.getUTCDate() ) +
+        'T' + pad( this.getUTCHours() ) +
+        ':' + pad( this.getUTCMinutes() ) +
+        ':' + pad( this.getUTCSeconds() ) +
+        '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
+        'Z';
+    };
+
+  }() );
+}
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ES5.1', '#sec-15.9.5.43', 'Date.prototype.toISOString')}}
+ Implemented in JavaScript 1.8
{{Spec2('ES5.1')}}Initial definition.
{{SpecName('ES6', '#sec-date.prototype.toisostring', 'Date.prototype.toISOString')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

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

+ +

相关链接

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