Javascript Date.toJSON没有得到时区偏移 [英] Javascript Date.toJSON don't get the timezone offset

查看:449
本文介绍了Javascript Date.toJSON没有得到时区偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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



我没有注意到 toJSON 方法没有考虑到您的时区偏移量,所以我已经结束了这个hacky解决方案:

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

有更优雅的解决方案吗?




解决方案

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



Date.prototype.toJSON 的规范说它使用 Date.prototype.toISOString ,其中指出时区总是UTC。您的解决方案是通过时区偏移来抵消日期对象的UTC时间值。



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

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



修改



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

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

但是,过早的优化,所以除非你打电话给数千次很短的时间,我不会打扰。


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

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

to get my date as YYYY-MM-DD string, 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).

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

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

Is there a more elegant solution?

解决方案

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

The specification for Date.prototype.toJSON says 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.

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

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.

这篇关于Javascript Date.toJSON没有得到时区偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆