javascript 使用 Moment.js 制作本周日期的数组不会添加到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28010754/
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
Using Moment.js to make an array of this week's dates doesn't add to array
提问by Chris
I'm using Moment.js to make a Resource Calendar and I need an array of dates for this week. The console log of my current function prints out appropriately but the array that gets pushed in for each date is wrong.
我正在使用 Moment.js 制作一个资源日历,我需要本周的一组日期。我当前函数的控制台日志正确打印出来,但为每个日期推送的数组是错误的。
var startOfWeek = moment().startOf('week');
var endOfWeek = moment().endOf('week');
var days = [];
var day = startOfWeek;
do {
console.log(day._d);
days.push(day._d);
day = day.add(1, 'd');
}
while (day <= endOfWeek);
console.log(days);
Returns:
返回:
Sun Jan 18 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Mon Jan 19 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Tue Jan 20 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Wed Jan 21 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Thu Jan 22 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Fri Jan 23 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Sat Jan 24 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
[Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST)]
Notice how the array at the bottom is the next date in the array repeated 7 times.
请注意底部的数组是如何重复 7 次的数组中的下一个日期。
回答by Taylor Buchanan
As danludwig mentioned in his comment on the question, you are adding a reference for the same date to the array multiple times.
正如 danludwig 在他对该问题的评论中提到的,您多次向数组添加同一日期的引用。
From the Moment.js documentation:
It should be noted that moments are mutable. Calling any of the manipulation methods will change the original moment.
If you want to create a copy and manipulate it, you should use moment#clone before manipulating the moment.
应该注意的是,时刻是可变的。调用任何操作方法都会改变原来的时刻。
如果要创建副本并对其进行操作,则应在操作时刻之前使用 moment#clone。
You should be calling the clone
function on the Moment date object as shown here.
你应该调用clone
的瞬间日期对象的功能,如图这里。
var startOfWeek = moment().startOf('week');
var endOfWeek = moment().endOf('week');
var days = [];
var day = startOfWeek;
while (day <= endOfWeek) {
days.push(day.toDate());
day = day.clone().add(1, 'd');
}
console.log(days);
As an aside:
作为旁白:
You should notreference internal fields/functions of a 3rd party library. The name of these references are more likely to change than the public API described in the documentation. _d
can be referenced by calling the public functiontoDate
.
您不应引用 3rd 方库的内部字段/函数。这些引用的名称比文档中描述的公共 API 更可能更改。_d
可以通过调用公共函数来引用toDate
。