角度材料Datepicker时区忽略?

时间:2017-09-06 11:02:18

标签: angularjs datepicker timezone angular-material

我正在尝试从用户获取航班日期,而datepicker始终将日期设置为具有用户时区的datetime对象,从而导致向服务器发送错误的日期。 试图使用ng-model-options="{ timezone: 'utc' }"

但是用户在选择日期后显示错误的日期(显示为用户选择日期之前的日期)。

如何告诉datepicker完全忽略时区 - 如果不可能,我可以在FE / BE上做什么来将此日期转换为非时区日期?

我知道我可以在FE上将其转换为Posix但是一旦我这样做,datepicker会尖叫它需要日期对象。

提前致谢...

1 个答案:

答案 0 :(得分:0)

我有完全相同的问题,我使用此代码结束了我们

Date.prototype.toJSON = function () {
   var timezoneOffsetInHours = -(this.getTimezoneOffset() / 60); //UTC minus local time
   var sign = timezoneOffsetInHours >= 0 ? '+' : '-';
   var leadingZero = (timezoneOffsetInHours < 10) ? '0' : '';

   //It's a bit unfortunate that we need to construct a new Date instance 
   //(we don't want _this_ Date instance to be modified)
   var correctedDate = new Date(this.getFullYear(), this.getMonth(), 
   this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), 
   this.getMilliseconds());
   correctedDate.setHours(this.getHours() + timezoneOffsetInHours);
   var iso = correctedDate.toISOString().replace('Z', '');

   return iso + sign + leadingZero + Math.abs(timezoneOffsetInHours).toString() + ':00';
}

来自其他SO问题How to JSON stringify a javascript Date and preserve timezone