'How to find future UTC offset based on specific date and time zone name using Angular?

Is there an equivalent for figuring out UTC offset by supplying date, time, time zone name using Angular? This is very easy in C# - example below:

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");

string dateInput = "2022-11-15";
var parsedDate = DateTime.Parse(dateInput);
TimeSpan offset = tzi.GetUtcOffset(parsedDate); // this returns -5

string dateInput2 = "2022-03-20";
var parsedDate2 = DateTime.Parse(dateInput2);
TimeSpan offset2 = tzi.GetUtcOffset(parsedDate2); // this returns -4


Solution 1:[1]

The JavaScript getTimezoneOffset() method is used to find the timezone offset

let d = new Date(Date.parse("2022-03-21T06:38:30+0000"));
console.log(d.getTimezoneOffset()) // 120 minutes offset for me

Solution 2:[2]

After many hours spent on this I decided to give moment.js as suggested by Joosep.P to try some library and also by others. The below seems to work as expected. What is bizarre is if you try to use timezoneName other than where you currently are and if you debug, you will still see your own time zone name for the date, however the offset will be calculated correctly. I assume that this works because of proper setting .tz call.

var timezoneName = 'America/New_York';
var dateInput = '2022-11-15';
var parsedDate = moment.tz(dateInput, timezoneName);
var offseta = parsedDate.utcOffset()/60; // this returns -5
var offsetb = moment.tz(dateInput, timezoneName).format('Z'); // returns '-05:00' as a string

var dateInput2 = '2022-03-20';
var parsedDate2 = moment.tz(dateInput2, timezoneName);
var offset2a = parsedDate2.utcOffset()/60; // this returns -4
var offset2b = moment.tz(dateInput2, timezoneName).format('Z'); // returns '-04:00' as a string

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2