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.
 
 

81 lines
2.2 KiB

var XTZ;
(function () {
"use strict";
if (!XTZ) {
try {
XTZ = require("xtz");
} catch (e) {
XTZ = require("./xtz.js");
}
}
var TZ = XTZ;
var tzDate;
//
// UTC-absolute time translated to a Time Zone
//
function demo1() {
console.info("What's the UTC equivalent of 8:15am in New York?");
console.info();
console.info("\t// during daylight savings");
console.info(`\tXTZ.fromTimeZone("2021-03-14 08:15:59.000", "America/New_York")`);
console.info(`\ttzDate.toISOString()`);
tzDate = XTZ.fromTimeZone("2021-03-14 08:15:59.000", "America/New_York");
console.info(
"\t" + tzDate.toISOString(),
"// same as",
new Date(tzDate.toISOString()).toISOString()
);
console.info();
console.info("\t// during standard time");
console.info(`\tXTZ.fromTimeZone("2021-11-07 08:15:59.000", "America/New_York")`);
console.info(`\ttzDate.toISOString()`);
tzDate = XTZ.fromTimeZone("2021-11-07 08:15:59.000", "America/New_York");
console.info(
"\t" + tzDate.toISOString(),
"// same as",
new Date(tzDate.toISOString()).toISOString()
);
console.info();
}
//
// Time Zone-relative time translated to UTC
//
function demo2() {
console.info("What time is it in New York at 8:15am on March 14th UTC?");
console.info();
console.info("\t// during daylight savings");
console.info(
`\tXTZ.toTimeZone("2021-03-14T08:15:59.000Z", "America/New_York")`
);
console.info(`\ttzDate.toISOString()`);
tzDate = XTZ.toTimeZone("2021-03-14T08:15:59.000Z", "America/New_York");
console.info(
"\t" + tzDate.toISOString(),
"// same as",
new Date(tzDate.toISOString()).toISOString()
);
console.info();
console.info("\t// during standard time");
console.info(`\tXTZ.fromTimeZone("2021-11-07T08:15:59.000Z", "America/New_York")`);
console.info(`\ttzDate.toISOString()`);
tzDate = XTZ.fromTimeZone("2021-11-07T08:15:59.000Z", "America/New_York");
console.info(
"\t" + tzDate.toISOString(),
"// same as",
new Date(tzDate.toISOString()).toISOString()
);
console.info();
}
demo1();
demo2();
})();