admin管理员组

文章数量:1244317

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)

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)

Share Improve this question asked Jun 22, 2013 at 0:08 evfwcqcgevfwcqcg 16.3k15 gold badges57 silver badges72 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

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.

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))

To do it 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/aczuV/1/

本文标签: javascriptConstruct an array of dates between start and end dates (d3js)Stack Overflow