Skip to content

Instantly share code, notes, and snippets.

@atk
Forked from 140bytes/LICENSE.txt
Created June 29, 2011 13:46
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Date.parse polyfill (no timezone support)
// 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]
)
}
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])}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Alex Kloss <alexthkloss@web.de>
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": "parsedate",
"description": "Date.parse replacement",
"keywords": [
"Date",
"parse"
]
}
<!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>
@atk
Copy link
Author

atk commented Jul 5, 2011

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.

@jdalton
Copy link

jdalton commented Aug 21, 2011

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