Here’s some sample code:
function isDSTTodayInThisTimeZone(timeZone) {
// This is the part I didn't know you could do in JS
// Cache results to avoid recalculating
if (!isDSTTodayInThisTimeZone.cache) {
isDSTTodayInThisTimeZone.cache = {};
}
if (isDSTTodayInThisTimeZone.cache[timeZone]) {
return isDSTTodayInThisTimeZone.cache[timeZone];
}
const now = new Date();
const januaryOffset = new Date(now.getFullYear(), 0, 1)
.toLocaleString("en-US", { timeZone, timeZoneName: "short" })
.split(" ").pop();
const julyOffset = new Date(now.getFullYear(), 6, 1)
.toLocaleString("en-US", { timeZone, timeZoneName: "short" })
.split(" ").pop();
const currentOffset = now
.toLocaleString("en-US", { timeZone, timeZoneName: "short" })
.split(" ").pop();
const isDST = currentOffset === Math.min(januaryOffset, julyOffset);
isDSTTodayInThisTimeZone.cache[timeZone] = isDST;
return isDST;
}
I didn’t know that you could store a function’s result in a cache belonging to that function. ChatGPT says you can and it seems to work.