codeandcloud
codeandcloud

Reputation: 55200

Why are the timezone offset returning different values?

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?

Upvotes: 4

Views: 704

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500515

JavaScript's getTimeZoneOffset returns 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 GetUtcOffset returns 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 TimeZoneInfo instead of TimeZone.

Upvotes: 6

Related Questions