-
-
Save atk/1053863 to your computer and use it in GitHub Desktop.
| // 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 <[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. |
| { | |
| "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> |
Yes, you are right. Changed.
@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.
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
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.
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.
Shouldn't April be "pr"?