add or subtract timezone difference to javascript Date
Asked Answered
T

6

64

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);
Thermoelectric answered 13/9, 2011 at 14:25 Comment(0)
C
98

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);
Consign answered 14/9, 2011 at 22:37 Comment(2)
this would be correct but what if it is daylight saving time in your country and now if your GMT was say +2, it would now be +1Thermoelectric
getTimezoneOffset accounts for daylight savings time. I am in the U.S. Eastern time zone, which is normally GMT-5, but GMT-4 in daylight savings. If I call getTimezoneOffset in my browser right now, I get 240, which is GMT-4. That, of course, only handles daylight savings for local time. The timezone offset from the DB should be adjusted for DST on the back end.Consign
J
36

Simple function that works for me:

adjustForTimezone(date:Date):Date{
    var timeOffsetInMS:number = date.getTimezoneOffset() * 60000;
    date.setTime(date.getTime() + timeOffsetInMS);
    return date
}
Jews answered 7/9, 2017 at 19:58 Comment(1)
it worked for me. but small changes on 3rd line date.setTime(date.getTime() + timeOffsetInMS); add + instead of -Stuffy
S
20

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)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

The getTimezoneOffset() method returns the difference, in minutes, between a date as evaluated in the UTC time zone, and the same date as evaluated in the local time zone.

So all you need is to compensate, IN MINUTES

Sporty answered 28/11, 2018 at 21:11 Comment(2)
I needed to get it back into a date so wrapped line 2 in new Date, i.e. new Date(dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset()))Colwell
Although for me the plus needed to be a minus. Brain not quite working as to why exactly.Colwell
B
3

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());
Bonaire answered 30/11, 2021 at 20:51 Comment(0)
A
2

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;
      } 
Ardelia answered 20/12, 2021 at 6:49 Comment(0)
I
0
adjustForTimezone(date){    
      var targetTime = new Date(date);
      // Manually construct a UTC date string to ensure time components are set consistently
      // This avoids timezone and DST issues entirely
      return new Date(Date.UTC(targetTime.getFullYear(), targetTime.getMonth(), targetTime.getDate()));
}
Imbecile answered 23/2 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.