Krishna Shetty
Krishna Shetty

Reputation: 1441

Timezone offset of any time using using JavaScript Date

I am using var offset = new Date().getTimezoneOffset() to get the current timezone offset of user.

But, how can I get the timezone offset for any future time?

This is required because the timezone offset is different when DST is enabled/disabled.So I cant assume the same offset for future time.

Upvotes: 0

Views: 1529

Answers (1)

lightsurge
lightsurge

Reputation: 21

This question confused me into thinking that perhaps getTimezoneOffset() wouldn't correctly get the timezone offset for future dates already. It does.

As far as JavaScript is concerned, you give it a milliseconds since the epoch date, which is ubiquitous, and it gives you the offset in local time as it will be on that date, because all the Date functionality cares about is taking a universal date/time and passing back a date that will make sense to the local user, unless you specifically ask it for the universal equivalent, with methods like getUTCDate().

So running

var summer = new Date(1403170847000);
alert('Summer offset=' + summer.getTimezoneOffset());

var winter = new Date(1419500175000);
alert('Winter offset=' + winter.getTimezoneOffset());

in a console where the local time includes a DST period, reports for me in the UK an offset of -60 (i.e. GMT+1) for the date in the summer, and 0 (i.e. GMT) for the date in the winter.

http://jsfiddle.net/SX9SN/

Upvotes: 2

Related Questions