6

我的代码中的以下行:

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

返回 -120.14933013916015。

我期望返回值是 120,并且通常,该值始终是一个整数。

谁能解释一下?现在看来我需要对结果进行一轮操作。

4

3 回答 3

1

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.
于 2013-11-05T18:27:52.260 回答
0

时区偏移是 UTC 和本地时间差异的结果,该值表示分钟,如果值为负,则偏移量在 UTC 之前,记住这一点简单的代码行可以为您提供偏移量传统的以小时为单位。

var offset = (new Date().getTimezoneOffset()/-60);
于 2014-04-10T02:31:00.123 回答
-1
var timezoneoffset        = new Date().getTimezoneOffset();
var timezoneoffsetinteger = parseInt(timezoneoffset);

变量 timezoneoffsetinteger 将等于 -120 作为整数。我猜你是在 GMT+2?

如果您希望它对 GMT+ 有利而对 GMT- 不利,则需要执行以下操作:

var timezoneoffsetconvert = timezoneoffsetinteger * (-2) / 2

这将返回(对于 GMT+2 (-120))120

于 2013-10-27T13:51:46.963 回答