toISOString
Summary
Returns a date as a string value in simplified ISO 8601 Extended format.
Syntax
toISOString()
Return Value
String in simplified ISO 8601 Extended format: YYYY-MM-DDTHH:mm:ss.sssZ
Examples
var now = new Date();
console.log(date.toISOString());
// ouputs: "2014-10-08T12:54:27.487Z"
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)
Usage
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
- toString Method (Date)
- toDateString Method (Date)
- toGMTString Method (Date)
- toLocaleString Method (Date)
- toLocaleDateString Method (Date)
- toLocaleTimeString Method (Date)
- toTimeString Method (Date)
- toUTCString Method (Date)
External resources
- toISOString(), by Mozilla Developer Network
- toISOString(), by Microsoft Developer Network
- Date Time String Format, ECMAScript 5.1
- Time Values and Time Range, ECMAScript 5.1
- ISO 8601, Wikipedia
Specification
ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011
Attributions
Microsoft Developer Network: Article