Skip to content

Instantly share code, notes, and snippets.

@atesgoral
Forked from 140bytes/LICENSE.txt
Created May 21, 2011 09:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save atesgoral/984398 to your computer and use it in GitHub Desktop.
Save atesgoral/984398 to your computer and use it in GitHub Desktop.
Parse UTC date string in ISO 8601 format
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
)
}
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))}
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.
{
"name": "parseIsoDate",
"keywords": [ "date", "parse", "iso8601" ]
}
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