I have two moment dates:
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?
If not, is there any better solution other than to make a loop which increments the fromDate
by 1 until it reaches the toDate
?
Edit: Adding date enumeration method and problem
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;
};
Take a look at the output when I run enumerateDaysBetweenDates( moment(new Date('1/1/2014')), moment(new Date('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) ]
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.
Momentjs doesn't provide this by itself but there is a plugin which offers it:
moment-range
.Specifically, check out the Iteration docs.
You can easily enumerate with
moment.js
Here is a more generic solution for days, weeks, months or years:https://gist.github.com/galioy/76f0d7b4b61b18fabfe9c0cc24fc3d2a
As an extension of Kyle's answer - I've been trying to get this to work with Unix timestamps and after lots of trial and error I got it to work and thought I'd post it here in case anyone is seeking the same thing and needs it. See my code below:
Note that I convert it to Unix, then convert that value to moment again. This was the issue that I had, you need to make it a moment value again in order for this to work.
Example usage:
Will return:
Got it for you:
Referencing
now
rather thanstartDate
made all the difference.If you're not after an inclusive search then change
.isSameOrBefore
to.isBefore
Fiddle: http://jsfiddle.net/KyleMuir/sRE76/118/
.add()
is a mutator method, so the assignment in this line is unnecessary:You can just do this, and have the same effect:
While it's name would imply the creation of a new
Date
object, thetoDate()
method really just returns the existing internalDate
object.So, none of your method calls are creating new
Date
ormoment
object instances. Fix that by using.clone()
to get a new instance: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.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: