Home > database >  how to show UTC date difference?
how to show UTC date difference?

Time:06-27

my Code:Here's what I have tried:

var d = new date();
let diff = d.getTimezoneOffset();   // -330

I am getting Difference as -330 minutes, how ever now current UTC difference is 330 minutes.

CodePudding user response:

getTimezoneOffset() returns the difference in minutes between UTC and your local time.

For example, if your time zone is GMT 2, -120 will be returned.

From the Documentation

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead.

If for some reason you need the opposite, like if you get -330 and you need 330, you can always do

diff *= -1; // This will invert the sign of the value.

CodePudding user response:

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.

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

From that you get: UTC - LocalDate = TimeZoneOffset.

As example: It's currently about 05:00 in Germany, we are GMT 2, therefore it's 03:00 in UTC.

3 - 5 = -2 * 60 = -120

That's really simple math. Basically you just need to *-1 the result...

  • Related