Javascript Moment.js-添加天数时的奇怪行为
我试图在10月(2016年)循环增加1天,但有一个奇怪的行为。2016年10月15日抵达时,不会增加1天,只有23小时Javascript Moment.js-添加天数时的奇怪行为,javascript,angularjs,momentjs,Javascript,Angularjs,Momentjs,我试图在10月(2016年)循环增加1天,但有一个奇怪的行为。2016年10月15日抵达时,不会增加1天,只有23小时 var date = moment("2016-09-25"); for (var j = 0; j < 42; j++) { console.log('before: ' + date.format()); date = date.clone(); date.add(1, 'day'); console.log('after: ' + d
var date = moment("2016-09-25");
for (var j = 0; j < 42; j++) {
console.log('before: ' + date.format());
date = date.clone();
date.add(1, 'day');
console.log('after: ' + date.format());
}
var日期=时刻(“2016-09-25”);
对于(var j=0;j<42;j++){
console.log('before:'+date.format());
date=date.clone();
日期。添加(1,'天');
console.log('after:'+date.format());
}
控制台:
之后:2016-10-13T00:00:00-03:00
2016-10-13T00:00:00-03:00之前
之后:2016-10-14T00:00:00-03:00
2016-10-14T00:00:00-03:00之前
之后:2016-10-15T00:00:00-03:00
2016-10-15T00:00:00-03:00之前
之后:2016-10-15T23:00:00-03:00
2016-10-15T23:00:00-03:00之前
之后:2016-10-16T23:00:00-02:00
2016-10-16T23:00:00-02:00之前
这是因为10月是夏令时生效的时候,因此你的时间会被抵消1小时
请阅读
时刻
的构造函数,并查看时刻.utc
和时刻.parseZone
,这是因为10月是夏时制生效的时间,因此将您的时间抵消1小时
请阅读
moment
的构造函数,并查看moment.utc
和moment.parseZone
这里的问题是巴西在午夜实行夏令时,这混淆了“一天”的概念。时刻试图做的是将日期设置为当前时间,日期=原始日期+1
问题在于,当它创建一个JS日期为2013-10-20T00:00:00时,底层的日期库会变得混乱,因为巴西不存在这个时间。不同浏览器的行为有所不同,但节点和Chrome中的行为如下:
d = moment('2016-10-19').toDate(); //get the native date object
d.setDate(18); //use the native API to set the date
d; // Fri Oct 18 2016 00:00:00 GMT-0300 (BRT), so works fine
//but
d = moment('2016-10-19').toDate(); //get the native date object
d.setDate(20);
d; // Sat Oct 19 2016 23:00:00 GMT-0300 (BRT), WTF?
我更改了endOf()的开始日期并解决了问题
date.endOf('day');
这里的问题是巴西在午夜实行夏令时,这混淆了“一天”的概念。时刻试图做的是将日期设置为当前时间,日期=原始日期+1 问题在于,当它创建一个JS日期为2013-10-20T00:00:00时,底层的日期库会变得混乱,因为巴西不存在这个时间。不同浏览器的行为有所不同,但节点和Chrome中的行为如下:
d = moment('2016-10-19').toDate(); //get the native date object
d.setDate(18); //use the native API to set the date
d; // Fri Oct 18 2016 00:00:00 GMT-0300 (BRT), so works fine
//but
d = moment('2016-10-19').toDate(); //get the native date object
d.setDate(20);
d; // Sat Oct 19 2016 23:00:00 GMT-0300 (BRT), WTF?
我更改了endOf()的开始日期并解决了问题
date.endOf('day');
Fiddle的控制台显示一个错误,说明,
时刻未定义。
:(欢迎来到DST世界。当夏令时在某些时区发生变化时。@DavidR在这里工作。试试jsbin:@VladimirM is spot on!@VladimirM omg!我忘了DSTwiddle的控制台显示一个错误,说明,时刻未定义。
:(欢迎来到DST的世界。当夏令时在某些时区发生变化时。@DavidR在这里工作。试试jsbin:@VladimirM是正确的!@VladimirM天哪!我忘记了DST