Get time offset (minutes) of timezone name
function getOffset(zoneName) {
let parts=new Intl.DateTimeFormat(
"en",{
timeZone:zoneName,hour12:false,day:"numeric",
hour:"numeric",minute:"numeric"
}
).formatToParts(new Date("2024-01-02T12:00Z"));
//change the month (01) according to your DST needs
let d=parts.find(e=>e.type=="day").value;
let h=parts.find(e=>e.type=="hour").value;
let n=parts.find(e=>e.type=="minute").value;
return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
};
You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.
Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):
function getOffset(date,zoneName) {
//date as Date object
//Date.UTC()
//new Date(ms_timestamp)
//Date.now()
//or "yyyy-mm-ddThh:nn:ssZ"
let parts=new Intl.DateTimeFormat(
"en",{
timeZone:zoneName,hour12:false,
year:"numeric",month:"2-digit,"day:"2-digit",
hour:"2-digit",minute:"2-digit",second:"2-digit",
fractionalSecondDigits:3
}
).formatToParts(new Date(date));
let y=parts.find(e=>e.type=="year").value;
let m=parts.find(e=>e.type=="month").value;
let d=parts.find(e=>e.type=="day").value;
let h=parts.find(e=>e.type=="hour").value;
let n=parts.find(e=>e.type=="minute").value;
let s=parts.find(e=>e.type=="second").value;
let f=parts.find(e=>e.type=="fractionalSecond").value;
let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"."+f+"Z");
return (date2.getTime()-date.getTime())/1000/60
};
Eventually, you will have to put it in a try catch block.
timeDiffToHere
ortimeDiffToUTC
).