add or subtract timezone difference to javascript Date

JavascriptDate

Javascript Problem Overview


What is the best approach to add or subtract timezone differences to the targetTime variable below. The GMT timezone values comes from the DB in this format: 1.00 for London time, -8.00 for Pacific time and so on.

Code looks like this:

date = "September 21, 2011 00:00:00";
targetTime = new Date(date);

Javascript Solutions


Solution 1 - Javascript

You can use Date.getTimezoneOffset which returns the local offset from GMT in minutes. Note that it returns the value with the opposite sign you might expect. So GMT-5 is 300 and GMT+1 is -60.

var date = "September 21, 2011 00:00:00";
var targetTime = new Date(date);
var timeZoneFromDB = -7.00; //time zone value from database
//get the timezone offset from local time in minutes
var tzDifference = timeZoneFromDB * 60 + targetTime.getTimezoneOffset();
//convert the offset to milliseconds, add to targetTime, and make a new Date
var offsetTime = new Date(targetTime.getTime() + tzDifference * 60 * 1000);

Solution 2 - Javascript

Simple function that works for me:

adjustForTimezone(date:Date):Date{
    var timeOffsetInMS:number = date.getTimezoneOffset() * 60000;
    date.setTime(date.getTime() + timeOffsetInMS);
    return date
}

Solution 3 - Javascript

If you need to compensate the timezone I would recommend the following snippet:

var dt = new Date('2018-07-05')
dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset())
console.log(dt)

Solution 4 - Javascript

This example shows how to use the local datetime but format it as ISO:

const d = new Date();

let dtOffset = new Date(d.setMinutes(d.getMinutes() - d.getTimezoneOffset()));
// Date in EST and ISO format: "2021-11-30T15:33:32.222Z"
console.log(dtOffset.toISOString());

Solution 5 - Javascript

Typescript version of @alexp answer

     adjustForTimezone(d:Date, offset:number):Date{
        var date = d.toISOString();
        var targetTime = new Date(date);
        var timeZoneFromDB = offset; //time zone value from database
        //get the timezone offset from local time in minutes
        var tzDifference = timeZoneFromDB * 60 + targetTime.getTimezoneOffset();
        //convert the offset to milliseconds, add to targetTime, and make a new Date
        var offsetTime = new Date(targetTime.getTime() + tzDifference * 60 * 1000);
        return offsetTime;
      } 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhalliewuudView Question on Stackoverflow
Solution 1 - JavascriptalexpView Answer on Stackoverflow
Solution 2 - Javascriptuser2875462View Answer on Stackoverflow
Solution 3 - JavascriptMarcos LimaView Answer on Stackoverflow
Solution 4 - Javascriptsmoore4View Answer on Stackoverflow
Solution 5 - JavascriptPritam RajputView Answer on Stackoverflow