Getting list of dates between two dates not returning correctly JS

前端 未结 3 1009
别那么骄傲
别那么骄傲 2021-01-28 09:23

I have a start date and an end date, and I want to generate a list of dates between (and including) these two dates. But I don\'t get why it isn\'t working...

I pass in

3条回答
  •  暖寄归人
    2021-01-28 10:02

    Try to add new Date(i), instead of just i:

    function dateList(dateStart, dateEnd) {
      var dates = [];
      for (i = dateStart; i <= dateEnd; i.setDate(i.getDate() + 1)){
        dates.push(new Date(i));
      }
      return dates;
    }
    
    console.log(dateList(new Date('2017-05-08'), new Date('2017-05-12')));

提交回复
热议问题