My location is at GMT +5:30
When I try to find getTimezoneOffset using JavaScript
var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
I get the value -5.5. Curiously, when I am doing the same using C#
var localZone = TimeZone.CurrentTimeZone;
var localOffset = localZone.GetUtcOffset(new Date());
var currentTimeZoneOffsetInHours = localOffset.TotalHours;
The return value is 5.5.
Is this sign change by design or am I missing anything important?
JavaScript’s
getTimeZoneOffsetreturns the offset to be added to local time to get to UTC. (The description of “the time-zone offset from UTC” is misleading, IMO.).NET’s
GetUtcOffsetreturns the offset to be added to the UTC time to get to local time, which is the more conventional approach IMO. It’s just a different point of reference, basically.Note that if you’re using .NET 3.5 or later, you should really be using
TimeZoneInfoinstead ofTimeZone.