-
-
Save 3rd-Eden/982725 to your computer and use it in GitHub Desktop.
JSON date parser
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
function( | |
a // The JSON encoded date string | |
) | |
{ | |
return ( // The returns only date if it can chunk it | |
a=/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).(\d+)/.exec(a) // Execute the chunking regular expression | |
) | |
, // last value behind the , is returned | |
new Date( // Create a new date | |
Date.UTC.apply( // with the UTC method | |
a[2]-- // Subtracts 1 from the month | |
,a.slice(1) // Removes the parsed string from the result | |
) | |
) | |
} |
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
var parser = function(a){return(a=/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).(\d+)/.exec(a)),new Date(Date.UTC.apply(a[2]--,a.slice(1)))} | |
, date = new Date | |
, jsondate = date.toJSON(); | |
console.log(parser(jsondate)); // Fri May 20 2011 14:26:43 GMT+0200 (CEST) | |
console.log(parser(jsondate).toJSON() == jsondate); // true | |
console.log(Object.prototype.toString.call(parser(jsondate))); // > [object Date] |
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
function(a){return(a=/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).(\d+)/.exec(a)),new Date(Date.UTC.apply(a[2]--,a.slice(1)))} |
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
Copyright (c) 2011 Arnout Kazemier, <http://blog.3rd-Eden.com> | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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": "jsondate", | |
"description": "Parses a JSON.stringify'd date back to a regular Date object", | |
"keywords": [ | |
"date", | |
"json", | |
"parse", | |
"stringify" | |
] | |
} |
Nope, it not supported by all browsers. For example, it does work in the latest Chrome but safari fails to parse that correctly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can't you parse JSON dates with just
new Date(JSON date string)
? What's the benefit of this?