简体   繁体   English

瞬间转换日期时间错误

[英]Moment converting datetime wrong

I'm getting time from a datepicker. 我从日期选择器中获取时间。 I select 15th july 2016 and when I check the variable of sd it is the 15th July 2016 00:00:00 GMT After I populate the filterDataSearchFilter.startDateTime variable after the next line it's now saying 14-july-2016, rather than the 15th. 我选择2016年7月15日,当我检查的变量sd这是2016年7月15日00:00:00 GMT后,我填充filterDataSearchFilter.startDateTime下一行之后变量它现在说的14月- 2016年,而不是15号。

Why is this happening? 为什么会这样呢? Why is moment giving me "2016-07-10T12:00:00.000Z" and not "2016-07-11T12:00:00.000Z" 为什么现在给我"2016-07-10T12:00:00.000Z"而不是"2016-07-11T12:00:00.000Z"

 goButtonSearchFilter() {
        debugger;
        if (this.filterDataSearchFilter.startDateTimeNonUtc != null) {
            var sd = this.filterDataSearchFilter.startDateTimeNonUtc.toString();
            this.filterDataSearchFilter.startDateTime = moment(this.filterDataSearchFilter.startDateTimeNonUtc).startOf('day').toISOString();
        }

The issues isn't with moment, it's the toISOString() which fails to consider the timezone offset. 问题不关乎时间,而是toISOString()无法考虑时区偏移。 Moment already gives you start of the day but isostring offsets the timezone therefore minuses that many hours, taking the day to previous one. Moment已经为您提供了一天的开始,但是等长线抵消了时区,因此减少了很多小时,使一天回到了前一天。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM