var range = getDates(new Date(), new Date().addDays(7));
I'd like "range" to be an array of date objects, one for each day between the two dates.
The trick is that it should handle month and year boundaries as well.
Thanks.
var range = getDates(new Date(), new Date().addDays(7));
I'd like "range" to be an array of date objects, one for each day between the two dates.
The trick is that it should handle month and year boundaries as well.
Thanks.
Just came across this question, the easiest way to do this is using moment:
You need to install moment and moment-range first:
Try this, remember to include moment js,
Here's a one liner that doesn't require any libraries in-case you don't want to create another function. Just replace startDate (in two places) and endDate (which are js date objects) with your variables or date values. Of course you could wrap it in a function if you prefer
d3js provides a lot of handy functions and includes d3.time for easy date manipulations
https://github.com/d3/d3-time
For your specific request:
Utc
or local time
range will be an array of date objects that fall on the first possible value for each day
you can change timeDay to timeHour, timeMonth etc for the same results on different intervals
Here is a functional demo http://jsfiddle.net/jfhartsock/cM3ZU/
Here's a canned method that will accept Moment dates or strings or a mixture as inputs and generate an array of dates as Moment dates. If you don't want Moment dates as output then change what the
map()
method returns.