Skip to content

Instantly share code, notes, and snippets.

@atk
Forked from 140bytes/LICENSE.txt
Created June 29, 2011 13:46
Show Gist options
  • Save atk/1053863 to your computer and use it in GitHub Desktop.
Save atk/1053863 to your computer and use it in GitHub Desktop.
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>
@jed
Copy link

jed commented Jun 29, 2011

nice work! i wonder if the 140bytes wizards can golf enough room in for Date.parse=Date.parse||... or timezones.

@eliperelman
Copy link

This should get you the polyfill in 139 bytes:

Date.parse=Date.parse||function(a){a=a.split(/[ :]\D?/);return Date(a[3],0|"anebarapayunulugepctovec".indexOf(a[1])/2,a[2],a[4],a[5],a[6])}

@atk
Copy link
Author

atk commented Jun 29, 2011

@eliperelman : Date without "new" returns the current date for me.
@jed : To extract the timezone, we could split ([+-]) too, but I don't see how to fit this in the rest of the bytes; we'd need to find out the difference to the current timezone, too and add/subtract it to/from the hours, too.

@eliperelman
Copy link

Hmm, interesting. Date() returns a string and new Date() returns an object. I'll look into it.

@jed
Copy link

jed commented Jun 29, 2011

i don't think you need the 0| anymore.

@atk
Copy link
Author

atk commented Jun 29, 2011

You're right, @jed Updated accordingly

@jed
Copy link

jed commented Jun 29, 2011

how about \W instead of [ :]?

@atk
Copy link
Author

atk commented Jun 29, 2011

Great idea - and since we haven't got special chars here, we can use search instead of indexOf, too.

@jed
Copy link

jed commented Jun 29, 2011

would still love to nail the timezone issue, i think it's more important than the assignment.

@atk
Copy link
Author

atk commented Jun 29, 2011

Problems to solve for timezone:

  • we lose the +/- of the timezone direction in the splitting atm
  • we need to compare to the current timezone :-(

especially the latter will be too long for the last 25 bytes.

@jed
Copy link

jed commented Jun 29, 2011

good points, @atk.

is there any potential in a function(a){return new Date(a.replace(...))} approach?

@atk
Copy link
Author

atk commented Jun 29, 2011

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:

function(a){return new Date(a.replace(/\w+ \w(\w+) (\d+) (\d+) (\d+):(\d+):(\d+) \w+(.\d+)/,function(a){a=arguments;[a[0],a[2]]=[a[2],a[0]];a[1]="anebarapayunulugepctovec".search(a[1])/2;return a.join(' ')}))}

Which does not yet take care of the timezone, btw.

@jed
Copy link

jed commented Jun 29, 2011

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)}

@atk
Copy link
Author

atk commented Jun 29, 2011

nice idea - I will look into it later.

@faisalman
Copy link

Shouldn't April be "pr"?

@atk
Copy link
Author

atk commented Jun 30, 2011

Yes, you are right. Changed.

@atk
Copy link
Author

atk commented Jun 30, 2011

@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.

@subzey
Copy link

subzey commented Jul 1, 2011

Maybe this will help somehow for timezones:
var timezone = -7;
var d = new Date();
d.setUTCFullYear(2011, 6, 1) /* 1 Jul 2011 */
d.setUTCHours(1 + timezone, 0, 0) /* 01:00:00 UTC -7 hours */
console.log(d.toUTCString());
// Thu, 30 Jun 2011 18:00:00 GMT
Works well in IE6+, Fx, Op, node.js

@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