Returns a date as a string value in simplified ISO 8601 Extended format.
SyntaxtoISOString()
Return Value
String in simplified ISO 8601 Extended format: YYYY-MM-DDTHH:mm:ss.sssZ
var now = new Date();
console.log(date.toISOString());
Manually assembling the ISO 8601 format (polyfill)
// if the environment does not support toISOString() yet
// add it to the Date prototype
if (!Date.prototype.toISOString) {
Date.prototype.toISOString = (function() {
function twoDigits(value) {
return (value < 10 ? '0' : ') + value;
}
return function toISOString() {
return this.getUTCFullYear()
+ '-' + twoDigits(this.getUTCMonth() + 1)
+ '-' + twoDigits(this.getUTCDate())
+ 'T' + twoDigits(this.getUTCHours())
+ ':' + twoDigits(this.getUTCMinutes())
+ ':' + twoDigits(this.getUTCSeconds())
+ '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5)
+ 'Z';
};
})();
}
Remarks Throws
RangeError
when called on a date object with an invalid date (e.g. new Date("I am not a date");
- see Time Values And Time Range)
The ISO 8601 Extended format is supported by Date.parse(), it is therefore a good choice when dates need to be exchanged between APIs.
See also Other articles
ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011
AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4