-
-
Save atk/1053863 to your computer and use it in GitHub Desktop.
Date.parse polyfill (no timezone support)
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
// Polyfill for Date.parse | |
Date.parse = Date.parse || function( | |
a // ISO Date string | |
){ | |
// turn into array, cutting the first character of the Month | |
a = a.split(/\W\D?/); | |
// create a new date object | |
return new Date( | |
// year | |
a[3], | |
// month (starting with zero) | |
// we got only the second and third character, so we find it in a string | |
// Jan => an => 0, Feb => eb => 1, ... | |
"anebarprayunulugepctovec".search(a[1]) / 2, | |
// day | |
a[2], | |
// hour | |
a[4], | |
// minute | |
a[5], | |
// second | |
a[6] | |
) | |
} |
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
Date.parse=Date.parse||function(a){a=a.split(/\W\D?/);return new Date(a[3],"anebarprayunulugepctovec".search(a[1])/2,a[2],a[4],a[5],a[6])} |
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 Alex Kloss <[email protected]> | |
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": "parsedate", | |
"description": "Date.parse replacement", | |
"keywords": [ | |
"Date", | |
"parse" | |
] | |
} |
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>Foo</title> | |
<div>Expected value: <b>Thu Mar 22 2007 12:56:06...</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var myFunction = Date.parse=Date.parse||function(a){a=a.split(/\W\D?/);return new Date(a[3],"anebarprayunulugepctovec".search(a[1])/2,a[2],a[4],a[5],a[6])} | |
document.getElementById( "ret" ).innerHTML = myFunction(''+new Date(2007,2,22,12,56,6)); | |
</script> |
Date.parse
has been around since ES1 making a fallback seems a bit out of place. Also the workings of ES5/5.1 Date.parse are a bit too complex to fit in 140bytes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
currently, I can cope for the given Date's timezone:
function(a){a=a.split(/[^\w-]\D?/);return new Date(a[3],"anebarprayunulugepctovec".search(a[1])/2,a[2],a[4]+(0|a[7].slice(2,5)),a[5],a[6])}
This doesn't take care of the local timezone, though - and I already got 139 bytes without the polyfill prefix.