Build an array of dates between start and end dates. (D3.js)

For example, I have start and end

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

How can I get from this 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 am reading Time-Scales docs now, but still have difficulty understanding how to use them to achieve this effect. (Or maybe there is a better way to do this, if so, I would be happy to know)

+4
source share
3 answers

Do this with d3:

 var dateArray = d3.time.scale() .domain([new Date(2013, 2, 28), new Date(2013, 3, 2)]) .ticks(d3.time.days, 1) 

Online demo: http://jsfiddle.net/aczuV/1/

+3
source

It just turned out that I can use range with time intervals

 d3.time.day.range(new Date(2013, 2, 28), new Date(2013, 3, 2 + 1)) 
+4
source

To update this answer for d3 V4 syntax , use:

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

Some important notes:

  • The month is indexed to 0, so 0 is January. This example will begin on March 28, not February 28.
  • The end date is not included in the return value. Thus, the last date in this example is the first, not the second.
+1
source

Source: https://habr.com/ru/post/1487580/


All Articles