I have the following function which I've written to convert msSinceEpoch to the New Zealand Date (IE11 Compatible)...
const MAGICNUMBER = 13;
const toNewZealand = (msSinceEpoch) => {
const [day, month, year, time] = new Date(msSinceEpoch).toLocaleString("en-NZ", {
hour12: false, timeZone: "UTC"
}).split(/[/,]/); // timeZone UTC is the only format support on IE11
const [hours, minutes, seconds] = time.trim().split(":");
return new Date(~~year, ~~month - 1, ~~day, ~~hours + MAGICNUMBER, ~~minutes, ~~seconds)
};
// usage....
console.log(
toNewZealand(new Date().getTime())
)
However, this contains a magic number which is not relative to New Zealand's daylight savings time (+12 or +13).
So here it gets complicated, how do I get the right number relative to daylight savings in New Zealand (+12 or +13).
My initial attempt was just to calculate whether it was in between the last Sunday of September or first Sunday of April but then I realised that the second I use a new Date() constructor anywhere in the code it's going to create a date relative to their system time and break the math.
TL;DR Convert UTC Milliseconds since epoch to New Zealand Time that works with New Zealand's Daylight savings settings.
EDIT: Also not interested in using Moment or any other library to solve this problem due to bundle size costs.