Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created August 23, 2012 19:49
Show Gist options
  • Select an option

  • Save nfreear/3440823 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/3440823 to your computer and use it in GitHub Desktop.
MDN: Date.toISOString / Feb 21, 2012 9:10:54 PM, MrDanielLewis; Public Domain; ECMAScript 5+; ISO 8601 extended; http://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Date/toISOString#Compatibility
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';
};
}() );
}
/*
Date.toISOString shim: Feb 21, 2012 9:10:54 PM, MrDanielLewis,
"Any copyright is dedicated to the Public Domain."
http://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Date/toISOString#Compatibility
http://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Date#Example.3A_ISO_8601_formatted_dates
*/
var today = new Date("05 October 2011 14:48 UTC");
alert(today.toISOString()); // Returns 2011-10-10T14:48:00.000Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment