This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

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

External resources

Specification

Date.prototype.toISOString()

ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011

Attributions

  • Microsoft Developer Network: Article