首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

date.toISOString

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

语法

代码语言:javascript
复制
dateObj.toISOString()

返回值

根据世界时间用ISO 8601格式给定日期的字符串。

例子

Using toISOString()

代码语言:javascript
复制
var today = new Date('05 October 2011 14:48 UTC');

console.log(today.toISOString()); // Returns 2011-10-05T14:48:00.000Z

上面的例子使用了非标准的字符串值的解析,可能在非Mozilla浏览器中不能正确解析。

代码语言:javascript
复制
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';
    };

  }());
}

规格

Specification

Status

Comment

ECMAScript 5.1 (ECMA-262)The definition of 'Date.prototype.toISOString' in that specification.

Standard

Initial definition. Implemented in JavaScript 1.8.

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Date.prototype.toISOString' in that specification.

Standard

ECMAScript Latest Draft (ECMA-262)The definition of 'Date.prototype.toISOString' in that specification.

Living Standard

浏览器兼容性

Feature

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

Basic Support

(Yes)

(Yes)

(Yes)

9

(Yes)

(Yes)

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

扫码关注腾讯云开发者

领取腾讯云代金券