Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct an array of dates between start and end dates. (d3.js)

For instance, I have start and end

start = new Date(2013, 2, 28)
end   = new Date(2013, 3, 2)

How could I get from this an array like this

[new Date(2013, 2, 28),
 new Date(2013, 2, 29),
 new Date(2013, 2, 30),
 new Date(2013, 2, 31),
 new Date(2013, 3, 1),
 new Date(2013, 3, 2)]

I'm currently reading docs about Time-Scales but still having trouble in understanding how to use them for achieving this effect. (Or, maybe there is a better way to do this, if so, I'd be happy to know)

like image 685
evfwcqcg Avatar asked Jun 22 '13 00:06

evfwcqcg


2 Answers

To update this answer for the d3 V4 syntax, use:

var dateRange = d3.timeDays(new Date(2013, 2, 28), new Date(2013, 3, 2));

Some important notes:

  • The month is 0-indexed, so 0 is January. This example will start on March 28th, not February 28th.
  • The end date is not included in the return value. Thus the last date in the this example is the 1st, not the 2nd.
like image 51
DavGarcia Avatar answered Sep 28 '22 16:09

DavGarcia


Just found out that I can use range with Time Intervals

d3.time.day.range(new Date(2013, 2, 28),
                  new Date(2013, 3, 2 + 1))
like image 35
evfwcqcg Avatar answered Sep 28 '22 16:09

evfwcqcg