Compare Revisions

Date.prototype.toISOString()

Change Revisions

Revision 51572:

Revision 51572 by Sheppy on

Revision 51573:

Revision 51573 by MrDanielLewis on

Title:
toISOString
toISOString
Slug:
JavaScript/Reference/Global_Objects/Date/toISOString
JavaScript/Reference/Global_Objects/Date/toISOString
Tags:
JavaScript, Date, "JavaScript Reference"
JavaScript, Date, "JavaScript Reference"
Content:

Revision 51572
Revision 51573
n14      The <code>Date.prototype.toISOString</code> is an ECMAScripn14      The <code>Date.prototype.toISOString</code> is an ECMAScrip
>t 5 addition. Format is as follows: <strong>YYYY-MM-DDTHH:mm:ss.s>t 5 addition. Format is as follows: <strong>YYYY-MM-DDTHH:mm:ss.s
>ssZ</strong><br>>ssZ</strong>
nn58    <h2 name="Description">
59      Compatibility
60    </h2>
61    <p>
62      This method was standardized in ECMA-262 5th edition.&nbsp;
 > Engines which have not been updated to support this method can w
 >ork around the absence of this method using the following shim:
63    </p>
64    <pre class="script-jem">
65if ( !Date.prototype.toISOString ) {
66    
67    ( function() {
68    
69        function pad(number) {
70            var r = String(number);
71            if ( r.length === 1 ) {
72                r = '0' + r;
73            }
74            return r;
75        }
76 
77        Date.prototype.toISOString = function() {
78            return this.getFullYear()
79                + '-' + pad( this.getUTCMonth() + 1 )
80                + '-' + pad( this.getUTCDate() )
81                + 'T' + pad( this.getUTCHours() )
82                + ':' + pad( this.getUTCMinutes() )
83                + ':' + pad( this.getUTCSeconds() )
84                + '.' + String( (this.getUTCMilliseconds()/1000).
 >toFixed(3) ).slice( 2, 5 )
85                + 'Z';
86        }
87  
88    }() );
89}
90</pre>
t63alert(today.toISOString()); // Returns 2011-10-10T14:48:00.000zt96alert(today.toISOString()); // Returns 2011-10-10T14:48:00.000Z

Back to History