Retrieve date between two dates

0

I need to make a for between two dates, entering each day of that period.

const firstDate = new Date(_visita.data);
const secondDate = new Date(_visita.dataSaida);
    
asked by anonymous 17.02.2018 / 16:58

1 answer

2

I suggest taking advantage of the Date builder by passing the value of the date that has added 1 day. The builder will make the respective adjustment at the end of the month, thus moving to the correct day of the following month. This way you can move forward in the days until you stop on the desired day.

Example (I changed the construction of your dates for simplicity):

const firstDate = new Date(2018, 1, 17);
const secondDate = new Date(2018, 2, 22);
let data = firstDate;

while (data <= secondDate){
  console.log(data); //utilizar a data para fazer algo
  data = new Date(data.getFullYear(), data.getMonth(), data.getDate() + 1);
  //                      ^--ano              ^--mês          ^--dia
}

Note that the last month in the constructor starts with 0 , so the 1 month is actually February.

    
17.02.2018 / 17:20