How to get time zone offset in javascript?


Here is the function:

 

function getTimeZoneOffset() {
var currentDate = new Date();
var date1 = new Date(currentDate.getFullYear(), 0, 1, 0, 0, 0, 0);
var temp = date1.toGMTString();
var date3 = new Date(temp.substring(0, temp.lastIndexOf(” “)));
var hoursDiffStdTime = ((date1 – date3) / (1000 * 60 * 60)) * 60;
return hoursDiffStdTime;
}

And if you want to get the offset with daylight saving then look at the below function:

 

function getTimeZoneOffsetDayLightSaving() {
var currentDate = new Date();
var date2 = new Date(currentDate.getFullYear(), 6, 1, 0, 0, 0, 0);
var temp = date2.toGMTString();
var date4 = new Date(temp.substring(0, temp.lastIndexOf(” “)));
var hoursDiffDaylightTime = ((date2 – date4) / (1000 * 60 * 60)) * 60;

return hoursDiffDaylightTime;
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: