Building an Array with the date range

2

I'll get two dates, Example: 10/01/2017, 10/31/2017. How can I create an array with every day between those dates.

Array = ["01/10/2017", "02/10/2017", "03/10/2017", ... ,"31/10/2017"]
    
asked by anonymous 16.10.2017 / 14:08

1 answer

4

The idea here is to add days to date using data.setDate . As long as the start date is smaller than the end date, it will be appended and included in the array . The two extra functions are to keep the dates in the Brazilian format.

let d1 = toDate("01/10/2017"),
    d2 = toDate("31/10/2017"),
    intervalos = [];

intervalos.push( toString(d1) );

while ( d1 < d2 ) {
  d1.setDate( d1.getDate() + 1 );
  intervalos.push( toString(d1) );
}

console.log(intervalos);

function toDate(texto) {
  let partes = texto.split('/');
  return new Date(partes[2], partes[1]-1, partes[0]);
}

function toString(date) {
  return ('0' + date.getDate()).slice(-2) + '/' +
    ('0' + (date.getMonth() + 1)).slice(-2) + '/' +
    date.getFullYear();
}
    
16.10.2017 / 14:17