// 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] | |
) | |
} |
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> |
This comment has been minimized.
This comment has been minimized.
This should get you the polyfill in 139 bytes:
|
This comment has been minimized.
This comment has been minimized.
@eliperelman : Date without "new" returns the current date for me. |
This comment has been minimized.
This comment has been minimized.
Hmm, interesting. |
This comment has been minimized.
This comment has been minimized.
i don't think you need the |
This comment has been minimized.
This comment has been minimized.
You're right, @jed Updated accordingly |
This comment has been minimized.
This comment has been minimized.
how about |
This comment has been minimized.
This comment has been minimized.
Great idea - and since we haven't got special chars here, we can use search instead of indexOf, too. |
This comment has been minimized.
This comment has been minimized.
would still love to nail the timezone issue, i think it's more important than the assignment. |
This comment has been minimized.
This comment has been minimized.
Problems to solve for timezone:
especially the latter will be too long for the last 25 bytes. |
This comment has been minimized.
This comment has been minimized.
good points, @atk. is there any potential in a |
This comment has been minimized.
This comment has been minimized.
Alas, not. Either would it need a regexp to parse the whole string at once, which is 53 bytes long, and/or it would need a callback which resolves the month name, too, a part which currently takes amazing 41 bytes, add the 2 function statements and the new Date() and you're well over 140 bytes. It would look something like this:
Which does not yet take care of the timezone, btw. |
This comment has been minimized.
This comment has been minimized.
i dunno, there's a bunch of stuff i thought was impossible before 140 bytes kicked off. what about something structured like this to reuse functions? function a(b,c,d,e,f,g){return c?XXXXX:b.replace(/YYYYYY/,a)} |
This comment has been minimized.
This comment has been minimized.
nice idea - I will look into it later. |
This comment has been minimized.
This comment has been minimized.
Shouldn't April be "pr"? |
This comment has been minimized.
This comment has been minimized.
Yes, you are right. Changed. |
This comment has been minimized.
This comment has been minimized.
@jed: I just found out that new Date([string]) internally calls Date.parse - so if it was not available anyway, we could only use eval for call will not work on new Date, thus enlargening the function quite a bit. Anyway, I will try to get Timezone handling inside this later. |
This comment has been minimized.
This comment has been minimized.
Maybe this will help somehow for timezones: |
This comment has been minimized.
This comment has been minimized.
currently, I can cope for the given Date's timezone:
This doesn't take care of the local timezone, though - and I already got 139 bytes without the polyfill prefix. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
nice work! i wonder if the 140bytes wizards can golf enough room in for
Date.parse=Date.parse||...
or timezones.