Psypher
Psypher

Reputation: 10829

JavaScript Time Zone Offset does not match Windows Offset

I am having an unique issue using JavaScript. When running the below code my system which is in timezone UTC -5 Eastern Standard Time I am getting the value as -4 UTC, it should have been -5 UTC.

Currently daylight saving is enabled but JavaScript seems to be unaware about it. What's wrong here, is there any other way to retrieve the correct offset?

var d = new Date();
var tz = d.toString().split("GMT")[1].split(" (")[0];
alert(tz);

Time and timezone from my system:

enter image description here

Upvotes: 2

Views: 2161

Answers (1)

robbmj
robbmj

Reputation: 16516

Right now (April 8, 2015) EST – Eastern Standard Time (-05:00 UTC) is not in effect, however, EDT – Eastern Daylight Time is (-04:00 UTC). The values being returned by the JavaScript Date object are correct.

The line "(UTC-05:00) Eastern Time (US & Canada)" in the Date and Time Dialog is misleading. That line is not stating the current offset, just the offset for the time zone when DST is not in effect.

Also you can simplify your JavaScript, there is no need to parse the return of toString(), in fact it is considered bad practice.

var offset = new Date().getTimezoneOffset();

document.write("UTC offset in minutes: " + offset 
               + ", offset in hours: " + offset / 60);


Just some food for thought.

I am not a date and time expert by any means but I generally find it helpful to think of a time zone as something different then a UTC offset.

From Wikipedia

A time zone is a region that has a uniform standard time for legal, commercial, and social purposes. It is convenient for areas in close commercial or other communication to keep the same time, so time zones tend to follow the boundaries of countries and their subdivisions.

From Wikipedia

The UTC offset is the difference in hours and minutes from Coordinated Universal Time (UTC) for a particular place and date.

This distinction is important, take the Canadian province of Saskatchewan for example.

From Wikipedia

The Canadian province of Saskatchewan is geographically located in the Mountain Time Zone. However, most of the province observes Central Standard Time year-round. As a result, it is effectively on daylight saving time (DST) year round, as clocks are not turned back an hour in autumn.

So here is an example of one time zone (Mountain Time Zone) having multiple UTC offsets at the same time. Or if you prefer Saskatchewan belongs to the Mountain Time Zone from March to November and the Central Time Zone for the rest of the year.

Upvotes: 4

Related Questions