-
-
Save atesgoral/984398 to your computer and use it in GitHub Desktop.
Parse UTC date string in ISO 8601 format
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 ( | |
s // ISO 8601 UTC date in yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format, | |
t // Placeholder | |
) { | |
t = /(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{3})Z/ | |
.exec(s) // Parse tokens | |
.slice(1); // Discard matched string | |
--t[1]; // Fix month | |
return new Date( | |
Date.UTC.apply(0, t) // get milliseconds since epoch for date components in UTC | |
) | |
} |
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(s,t){t=/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{3})Z/.exec(s).slice(1);--t[1];return new Date(Date.UTC.apply(0,t))} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Ates Goral <http://magnetiq.com> | |
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. |
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": "parseIsoDate", | |
"keywords": [ "date", "parse", "iso8601" ] | |
} |
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 parseIsoDate = function(s,t){t=/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{3})Z/.exec(s).slice(1);--t[1];return new Date(Date.UTC.apply(0,t))}; | |
console.log(parseIsoDate("2011-05-21T05:23:42.123Z").toUTCString()); | |
// Sat, 21 May 2011 05:23:42 GMT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment