Skip to content

Instantly share code, notes, and snippets.

@defims
Forked from atesgoral/LICENSE.txt
Created June 13, 2016 08:09
Show Gist options
  • Select an option

  • Save defims/0a6e22a4aac2dd2a0a189e5ee2fa749a to your computer and use it in GitHub Desktop.

Select an option

Save defims/0a6e22a4aac2dd2a0a189e5ee2fa749a 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
)
}
export 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" ],
"version": "0.1.0"
}
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