Javascript - get array of dates between 2 dates

后端 未结 25 1658
傲寒
傲寒 2020-11-22 15:16
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

25条回答
  •  情书的邮戳
    2020-11-22 15:47

    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

    var range = d3.utcDay.range(new Date(), d3.utcDay.offset(new Date(), 7));
    

    or local time

    var range = d3.timeDay.range(new Date(), d3.timeDay.offset(new Date(), 7));
    

    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

提交回复
热议问题