-
-
Save jed/1044533 to your computer and use it in GitHub Desktop.
ES5-ish shim for Date.prototype.toISOString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// thanks to @fgnass and @subzey for their awesome golf skills | |
// annotation by @fgnass | |
function(a){ | |
a=this; | |
return ( | |
1e3 // Insert a leading zero as padding for months < 10 | |
-~a.getUTCMonth() // Months start at 0, so increment it by one | |
*10 // Insert a trailing zero as padding for days < 10 | |
+a.toUTCString() // Can be "1 Jan 1970 00:00:00 GMT" or "Thu, 01 Jan 1970 00:00:00 GMT" | |
+1e3+a/1 // Append the millis, add 1000 to handle timestamps <= 999 | |
// The resulting String for new Date(0) will be: | |
// "-1010 Thu, 01 Jan 1970 00:00:00 GMT1000" or | |
// "-10101 Jan 1970 00:00:00 GMT1000" (IE) | |
).replace( | |
// The two digits after the leading '-1' contain the month | |
// The next two digits (at whatever location) contain the day | |
// The last three chars are the milliseconds | |
/1(..).*?(\d\d)\D+(\d+).(\S+).*(...)/, | |
'$3-$1-$2T$4.$5Z') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a){a=this;return(1e3-~a.getUTCMonth()*10+a.toUTCString()+1e3+a/1).replace(/1(..).*?(\d\d)\D+(\d+).(\S+).*(...)/,'$3-$1-$2T$4.$5Z')} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "toISOString", | |
"description": "ES5-ish shim for Date.prototype.toISOString", | |
"contributors": ["@subzey", "@fgnass"], | |
"keywords": [ | |
"ES5", | |
"shim", | |
"Date", | |
"toISOString" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Date.prototype.toISOString</title> | |
<div>Expected value: <b id="expected"></b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var toISOString = function(a){a=this;return(1e3-~a.getUTCMonth()*10+a.toUTCString()+1e3+a/1).replace(/1(..).*?(\d\d)\D+(\d+).(\S+).*(...)/,'$3-$1-$2T$4.$5Z')} | |
, now = new Date | |
document.getElementById( "expected" ).innerHTML = Date.prototype.toISOString.call(now) | |
document.getElementById( "ret" ).innerHTML = toISOString.call(now) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wouldn't mind calling this function "ES5 compliant" as the function itself behaves as expected in all tested browsers.
We should add a big warning though, that explains that there are better (read more future-proof) ways to implement this, but which unfortunately don't fit into 140 bytes.
Perhaps @jed could create another Gist with his original implementation and link to it, so that others can try to golf enough bytes out of it. Even if that might be impossible, it would still be a good documentation resource.