A fast, lightweight, zero-dependency library to translate between Time Zones and UTC with native Intl.DateTimeFormat in ~100 LoC.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

50 lines
1.1 KiB

(function (exports) {
"use strict";
// See <https://github.com/coolaj86/AJScript/issues/27>
function toLocalISOString(dateOrStr) {
var d;
if (dateOrStr) {
d = new Date(dateOrStr);
} else {
d = new Date();
}
var YYYY = d.getFullYear();
var MM = p2(d.getMonth() + 1);
var DD = p2(d.getDate());
var hh = p2(d.getHours());
var mm = p2(d.getMinutes());
var ss = p2(d.getSeconds());
var sss = d.getMilliseconds().toString().padStart(3, "0");
var offset = formatOffset(-d.getTimezoneOffset());
return `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}.${sss}${offset}`;
}
function formatOffset(minutes) {
if (!minutes) {
return "Z";
}
var h = Math.floor(Math.abs(minutes) / 60);
var m = Math.abs(minutes) % 60;
var offset = "";
if (minutes > 0) {
offset = "+";
} else if (minutes < 0) {
offset = "-";
}
// +0500, -0730
return offset + p2(h) + p2(m);
}
function p2(x) {
return String(x).padStart(2, "0");
}
exports.toLocalISOString = toLocalISOString;
})(("undefined" === typeof module && window) || exports);