javascript 烦人的javascript时区调整问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7629286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:45:00  来源:igfitidea点击:

Annoying javascript timezone adjustment issue

javascriptajaxjsoniso8601

提问by garyewe

I have set up a JSON endpoint that returns the current time from server. For example:

我已经设置了一个从服务器返回当前时间的 JSON 端点。例如:

{
  "myservertime": "2011-10-02T23:00+02:00"
}

So this is the CET summer time right now.

所以现在是 CET 夏令时。

Now, I also have a jQuery code that parses that very well.

现在,我还有一个 jQuery 代码可以很好地解析它。

$.sysTime = function(success) {

            $.ajax({
                url: '/jsontimepath/',
                dataType: 'json',
                async: false,
                success: function(json){
                    sysDateTime = new Date(Date.parse(json.myservertime));
                    console.log('The system time now is: ' + sysDateTime)
                }
            });

            return sysDateTime;
        };  

The problem is that when I check the console, it still shows wrong time... It is still affected by the timezone of my computer... For example, for a user in Hong Kong, the time quoted above would result:

问题是当我查看console的时候,还是显示错误的时间……还是受我电脑时区的影响……比如对于香港的用户,上面引用的时间会导致:

Mon Oct 03 2011 05:00:00 GMT+0800 (HKT)

2011 年 10 月 3 日星期一 05:00:00 GMT+0800 (HKT)

I do give it a valid ISO8601 time string and it just adjusts it. The actual time that is returned is correct (in that timezone)... But why does it adjust it like that??? I want it to return CET time not the local time...

我确实给了它一个有效的 ISO8601 时间字符串,它只是调整它。返回的实际时间是正确的(在那个时区)......但为什么它会这样调整???我希望它返回 CET 时间而不是当地时间...

回答by Tomasz Nurkiewicz

Everything is fine, try this:

一切正常,试试这个:

new Date(Date.parse("2011-10-02T23:00+02:00")).getUTCHours()  //21

The date is parsed correctly (taking the timezone into account as expected). However when you simply print Date.toString()it shows the date in current browser timezone (one of the sins of Java Dateobject shamelessly copied to JavaScript...)

日期被正确解析(按预期考虑时区)。但是,当您简单地打印时,Date.toString()它会显示当前浏览器时区中的日期(JavaDate对象无耻地复制到 JavaScript的罪过之一......)

If you stick to getUTC*()family of methods you will get correct values (like in example above). The ordinary get*()methods are always affected by browser timezone (and not the timezone from the date you parsed, which is lost), hence often useless.

如果您坚持使用getUTC*()系列方法,您将获得正确的值(如上例所示)。普通get*()方法总是受浏览器时区的影响(而不是从您解析的日期开始的时区,这是丢失的),因此通常无用。

Another example: the 2011-10-03 02:00+03:00is actually 23:00 on 2nd of October. But when you parse it (my current browser time zone is +0200 (CEST)):

另一个例子:2011-10-03 02:00+03:00实际上是 10 月 2 日的 23:00。但是当你解析它时(我当前的浏览器时区是 +0200 (CEST)):

new Date(Date.parse("2011-10-03T02:00+03:00"))  //Oct 03 01:00:00 GMT+0200

However current day of month in UTC is:

然而,UTC 中的当前日期是:

new Date(Date.parse("2011-10-03T02:00+03:00")).getUTCDate()  //2 (2nd of Oct)