Javascript - get array of dates between 2 dates

后端 未结 25 1630
傲寒
傲寒 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:33

    Just came across this question, the easiest way to do this is using moment:

    You need to install moment and moment-range first:

    const Moment = require('moment');
    const MomentRange = require('moment-range');
    const moment = MomentRange.extendMoment(Moment);
    
    const start = moment()
    const end = moment().add(2, 'months')
    const range = moment.range(start, end)
    const arrayOfDates = Array.from(range.by('days'))
    console.log(arrayOfDates)
    

提交回复
热议问题