Javascript Date.toJSON 不获取时区偏移量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11382606/
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 12:59:55  来源:igfitidea点击:

Javascript Date.toJSON don't get the timezone offset

javascriptdatetimezone

提问by simo

Well the problem is that I was using code like this:

那么问题是我正在使用这样的代码:

new Date().toJSON().slice(0, 10)

to get my date as YYYY-MM-DDstring, then I use it like parameter in some mysql queries and in some condition statements. In the end of the day I wasn't getting the right date since it was still in the previous day (my timezone offset is +2/3 hours).

将我的日期作为YYYY-MM-DD字符串,然后我在一些 mysql 查询和一些条件语句中使用它像参数一样。在一天结束时,我没有得到正确的日期,因为它仍然是前一天(我的时区偏移量是 +2/3 小时)。

I haven't noticed that the toJSONmethod does not take into account your timezone offset, so I've ended up with this hacky solution:

我没有注意到该toJSON方法没有考虑您的时区偏移量,所以我最终得到了这个 hacky 解决方案:

var today = new Date();
today.setHours( today.getHours()+(today.getTimezoneOffset()/-60) );
console.log(today.toJSON().slice(0, 10));

Is there a more elegant solution?

有没有更优雅的解决方案?

回答by RobG

Date objects in ECMAScript are internally UTC. The timezone offset is used for local times.

ECMAScript 中的日期对象在内部是 UTC。时区偏移用于本地时间。

The specification for Date.prototype.toJSONsays that it uses Date.prototype.toISOString, which states that "the timezone is always UTC". What your solution is doing is offsetting the UTC time value of the date object by the timezone offset.

Date.prototype.toJSON的规范说它使用Date.prototype.toISOString,它指出“时区始终是 UTC”。您的解决方案正在做的是将日期对象的 UTC 时间值偏移时区偏移量。

Consider adding your own method to Date.prototype, e.g.

考虑将您自己的方法添加到 Date.prototype,例如

Date.prototype.toJSONLocal = function() {
  function addZ(n) {
    return (n<10? '0' : '') + n;
  }
  return this.getFullYear() + '-' + 
         addZ(this.getMonth() + 1) + '-' + 
         addZ(this.getDate());
} 

Edit

编辑

If you want to squeeze extra performance, the following should be faster:

如果你想挤压额外的性能,以下应该更快:

Date.prototype.toJSONLocal = (function() {
    function addZ(n) {
        return (n<10? '0' : '') + n;
    }
    return function() {
      return this.getFullYear() + '-' +
             addZ(this.getMonth() + 1) + '-' +
             addZ(this.getDate());
    };
}())

But that smacks of premature optimisation, so unless you are calling it thousands of times in a very short period, I wouldn't bother.

但这有点过早优化的味道,所以除非你在很短的时间内调用它数千次,否则我不会打扰。

回答by pozs

I haven't noticed that the toJSON method don't take into account your timezone offset

我没有注意到 toJSON 方法没有考虑您的时区偏移

But it does, it converts the local-time to Zulu (look at the end of the string: Z)

但确实如此,它将本地时间转换为祖鲁语(查看字符串的末尾:Z)

new Date( "2012-01-02T03:04:05+02:00" ).toJSON()
"2012-01-02T01:04:05.000Z"