date.toISOString

date.toISOString

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

语法

dateObj.toISOString()

返回值

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

例子

Using toISOString()

var today = new Date('05 October 2011 14:48 UTC' console.log(today.toISOString() // Returns 2011-10-05T14:48:00.000Z

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

Polyfill

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'; }; }() }

规格

SpecificationStatusComment
ECMAScript 5.1 (ECMA-262)The definition of 'Date.prototype.toISOString' in that specification.StandardInitial 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

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support(Yes)(Yes)(Yes)9(Yes)(Yes)

FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)