Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created June 24, 2011 10:06
Show Gist options
  • Save jed/1044533 to your computer and use it in GitHub Desktop.
Save jed/1044533 to your computer and use it in GitHub Desktop.
ES5-ish shim for Date.prototype.toISOString
// 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')
}
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')}
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.
{
"name": "toISOString",
"description": "ES5-ish shim for Date.prototype.toISOString",
"contributors": ["@subzey", "@fgnass"],
"keywords": [
"ES5",
"shim",
"Date",
"toISOString"
]
}
<!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>
Copy link

ghost commented Aug 24, 2011

@jed Thank you for clarifying your position. All your points are valid, with the exception of the first: the description of this Gist reads, "ES5 shim for Date.prototype.toISOString." This snippet does not aspire to be an ES 5 shim, as you have made clear; thus, I respectfully suggest updating the title accordingly.

Edit: Thank you for updating the title.

@fgnass
Copy link

fgnass commented Aug 25, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment