如何在Moment中枚举两个日期之间的日期 [英] How to enumerate dates between two dates in Moment
问题描述
我有两个时刻日期:
var fromDate = moment(new Date('1/1/2014'));
var toDate = moment(new Date('6/1/2014'));
片刻是否提供了枚举这两个日期之间所有日期的方法?
Does moment provide a way to enumerate all of the dates between these two dates?
如果没有,除了制作一个循环,将 fromDate
增加1之前,还有更好的解决方案吗?到达 toDate
?
If not, is there any better solution other than to make a loop which increments the fromDate
by 1 until it reaches the toDate
?
编辑:添加日期枚举方法和问题
我已经嘲笑了一个枚举两个日期之间日期的方法,但我遇到了一个问题。
I've mocked up a method for enumerating the days between two dates, but I'm running into an issue.
var enumerateDaysBetweenDates = function(startDate, endDate) {
var dates = [];
startDate = startDate.add(1, 'days');
while(startDate.format('M/D/YYYY') !== endDate.format('M/D/YYYY')) {
console.log(startDate.toDate());
dates.push(startDate.toDate());
startDate = startDate.add(1, 'days');
}
return dates;
};
当我运行 enumerateDaysBetweenDates时,看看输出(时刻(新的日期) ('1/1/2014')),时刻(新日期('1/5/2014'));
Thu Jan 02 2014 00:00:00 GMT-0800 (PST)
Fri Jan 03 2014 00:00:00 GMT-0800 (PST)
Sat Jan 04 2014 00:00:00 GMT-0800 (PST)
[ Sun Jan 05 2014 00:00:00 GMT-0800 (PST),
Sun Jan 05 2014 00:00:00 GMT-0800 (PST),
Sun Jan 05 2014 00:00:00 GMT-0800 (PST) ]
它是console.logging正确的日期,但只有最后的日期被添加到数组。这是怎么/为什么?这闻起来像某种变量引用问题 - 但我没有看到它。
It's console.logging the right dates, but only the final date is being added to the array. How/why is this? This smells like some sort of variable reference issue - but I'm not seeing it.
推荐答案
.add()
是一种mutator方法,因此不必在此行中进行分配:
.add()
is a mutator method, so the assignment in this line is unnecessary:
startDate = startDate.add(1, 'days');
你可以这样做,并产生同样的效果:
You can just do this, and have the same effect:
startDate.add(1, 'days');
虽然它的名字意味着创建一个新的 Date
对象, toDate()
方法实际上只返回现有的内部日期
对象。
While it's name would imply the creation of a new Date
object, the toDate()
method really just returns the existing internal Date
object.
所以,没有您的方法调用正在创建新的日期
或时刻
对象实例。使用 .clone()
获取新实例:
So, none of your method calls are creating new Date
or moment
object instances. Fix that by using .clone()
to get a new instance:
startDate = startDate.clone().add(1, 'days');
或者更好的是,将值包装在时刻()
Mtz 在评论中建议,如果值是片刻,它将克隆实例对象,或者它将解析输入以创建新的时刻实例。
Or better yet, wrap the values in a call to moment()
as Mtz suggests in a comment, and it will clone the instance, if the value is a moment object, or it will parse the input to create a new moment instance.
startDate = moment(startDate).add(1, 'days');
我认为日期枚举器方法不应该改变传入的任何参数。我会创建一个用于枚举的单独变量。我也直接比较日期,而不是比较字符串:
I think a date enumerator method should not change either of the arguments passed in. I'd create a separate variable for enumerating. I'd also compare the dates directly, rather than comparing strings:
var enumerateDaysBetweenDates = function(startDate, endDate) {
var dates = [];
var currDate = moment(startDate).startOf('day');
var lastDate = moment(endDate).startOf('day');
while(currDate.add(1, 'days').diff(lastDate) < 0) {
console.log(currDate.toDate());
dates.push(currDate.clone().toDate());
}
return dates;
};
这篇关于如何在Moment中枚举两个日期之间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!