Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date.getTimezoneOffset returns a non integer value

The following line in my code:

var timezoneoffset = new Date().getTimezoneOffset();

returns -120.14933013916015.

I was expecting that the return value will be 120, and in general, that the value is always an integer.

Can anyone explain this? Right now it seems like I'll need to perform a round operation on the result.

like image 316
galbarm Avatar asked Oct 27 '13 12:10

galbarm


People also ask

What is 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.

Can timezone offset be negative?

Offset from Coordinated Universal Time. Offset from Coordinated Universal Time represents the difference in hours and minutes between UTC and the local system time. A negative offset indicates that the time is west of UTC and a positive offset indicates that the time is east of UTC.

How do I get current timezone in typescript?

To get the current browser's time zone, you can use the getTimezoneOffset() method from the JavaScript Date object. The getTimezoneOffset() returns the time difference, in minutes, between UTC time and local time.


1 Answers

I have tried everything I can think of, and cannot reproduce the problem you are reporting.

The only thing I can think of is that something else in your code is modifying the Date prototype. Perhaps you are using some date/time library that you didn't tell us about?

Please try reproducing the problem in a clean empty console.

  • Type about:blank in the URL bar
  • Press F12 for developer tools
  • Go to the "Console" tab
  • Type new Date().getTimezoneOffset() into the console.

Does it still show the decimals?

  • If yes, then there's a bug in IE or Windows
  • If no, then something in your other code was mucking with the Date prototype.
like image 199
Matt Johnson-Pint Avatar answered Oct 05 '22 21:10

Matt Johnson-Pint