Date.prototype.toISOString()

  • Revision slug: JavaScript/Reference/Global_Objects/Date/toISOString
  • Revision title: toISOString
  • Revision id: 51575
  • Created:
  • Creator: MrDanielLewis
  • Is current revision? No
  • Comment Hopefully this time the shim is in the right format; one or more formatting changes

Revision Content

Summary

JavaScript provides a direct way to convert a date object into a string in ISO format, the ISO 8601 Extended Format.

The Date.prototype.toISOString is an ECMAScript 5 addition. Format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

Method of Date
Implemented in JavaScript 1.8
ECMAScript Edition ECMAScript 5th Edition

Syntax

myDate.toISOString()

Note: mydate is a Date object.

Parameters

None.

Compatibility

This method was standardized in ECMA-262 5th edition.  Engines which have not been updated to support this method can work around the absence of this method using the following shim:

if ( !Date.prototype.toISOString ) {
    
    ( function() {
    
        function pad(number) {
            var r = String(number);
            if ( r.length === 1 ) {
                r = '0' + r;
            }
            return r;
        }
 
        Date.prototype.toISOString = function() {
            return this.getUTCFullYear()
                + '-' + pad( this.getUTCMonth() + 1 )
                + '-' + pad( this.getUTCDate() )
                + 'T' + pad( this.getUTCHours() )
                + ':' + pad( this.getUTCMinutes() )
                + ':' + pad( this.getUTCSeconds() )
                + '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
                + 'Z';
        };
  
    }() );
}

Examples

var today = new Date("05 October 2011 14:48 UTC");
alert(today.toISOString()); // Returns 2011-10-10T14:48:00.000Z

See Also

toUTCString()

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>JavaScript provides a direct way to convert a date object into a string in ISO format, the <a class="external" href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601 Extended Format</a>.</p>
<p>The <code>Date.prototype.toISOString</code> is an ECMAScript 5 addition. Format is as follows: <strong>YYYY-MM-DDTHH:mm:ss.sssZ</strong></p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Date" title="en/JavaScript/Reference/Global_Objects/Date"><code>Date</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.8</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<p><code>myDate.toISOString() </code></p>
<div class="note"><strong>Note:</strong> <code>mydate</code> is a <a href="/en/JavaScript/Reference/Global_Objects/Date" title="en/JavaScript/Reference/Global_Objects/Date"><code>Date</code></a> object.</div>
<h2 id="Parameters" name="Parameters">Parameters</h2>
<p>None.</p>
<h2 id="Description" name="Description">Compatibility</h2>
<p>This method was standardized in ECMA-262 5th edition.  Engines which have not been updated to support this method can work around the absence of this method using the following shim:</p> <pre class="brush: js">if ( !Date.prototype.toISOString ) {
    
    ( function() {
    
        function pad(number) {
            var r = String(number);
            if ( r.length === 1 ) {
                r = '0' + r;
            }
            return r;
        }
 
        Date.prototype.toISOString = function() {
            return this.getUTCFullYear()
                + '-' + pad( this.getUTCMonth() + 1 )
                + '-' + pad( this.getUTCDate() )
                + 'T' + pad( this.getUTCHours() )
                + ':' + pad( this.getUTCMinutes() )
                + ':' + pad( this.getUTCSeconds() )
                + '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
                + 'Z';
        };
  
    }() );
}
</pre>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: js">var today = new Date("05 October 2011 14:48 UTC");
alert(today.toISOString()); // Returns 2011-10-10T14:48:00.000Z
</pre>
<h2 id="See_Also" name="See_Also">See Also</h2>
<p><code><a href="/en/JavaScript/Reference/Global_Objects/Date/toUTCString" title="toUTCString">toUTCString()</a></code></p>
Revert to this revision