It is necessary that these dates be broken into components, that is
if 11/20/2016 to 01/20/2017
that is '20 .11.2016 ',' 21 .11.2016 ',' 22 .11.2016 '.... and so on until '19 .01.2017', '20 .01.2017'

in the form of js

I tried

var d = new Date(2016, 10, 20); var end = new Date(2017, 01, 20); var dates = []; while (d <= end) { dates.push(d.toLocaleDateString()); d.setDate(d.getDate() + 1); } 

but it fails. With new Date(2017, 01, 20) it shows not as the first month, as the second

  • What have you tried? What did not work out? - Alexey Shimansky
  • @ Alexey Shimansky var d = new Date (2016, 10, 20); var end = new Date (2017, 01, 20); var dates = []; while (d <= end) {dates.push (d.toLocaleDateString ()); d.setDate (d.getDate () + 1); } - kursof
  • @ AlekseyShimansky cannot be derived as in my description - kursof
  • @ Alexey Shimansky understand new Date (2017, 01, 20) it shows not as the first month but as the second - kursof
  • parseInt () can be used - Alexey Shimansky

2 answers 2

 var start = '20.11.2016', end = '20.01.2017'; var stArr = start.split('.'), endArr = end.split('.'); var daysArray = []; var date = new Date(stArr[2], parseInt(stArr[1]) - 1, parseInt(stArr[0])); while (true) { var year = date.getFullYear(), month = date.getMonth(), day = date.getDate(); daysArray.push(("0" + day).slice(-2) + '.' + ("0" + (month + 1)).slice(-2) + '.' + year); date.setDate(date.getDate() + 1); if (day == endArr[0] && month == endArr[1] - 1 && year == endArr[2]) { break; } } console.log(daysArray); 

here

("0" + day).slice(-2) - adds a leading zero if there is a number from 1 to 9 in the bottom

("0" + (month + 1)).slice(-2) - adds the leading zero, if there is a number from 1 to 9 in the month, and also adds 1, because months are counting from scratch

in the line var date = new Date(stArr[2], parseInt(stArr[1]) - 1, parseInt(stArr[0])); parseInt is done if there is a leading zero in the day or month number This way it will be circumcised. Plus, parseInt(stArr[1]) - 1 specified as the month; the month number is counted from zero.

     var start = new Date(2016, 10, 20), end = new Date(2017, 01, 20), between = [], year, month, day; while (start <= end) { var d = (!d) ? start.getDate() : 1; while (true) { if (d != new Date(start.getFullYear(), start.getMonth() - 1, d).getDate() || (start.getFullYear() == end.getFullYear() && start.getMonth() == end.getMonth() && d > end.getDate())) { break; } year = (start.getMonth() == 0) ? start.getFullYear() - 1 : start.getFullYear(); month = (start.getMonth() == 0) ? 12 : ("0" + start.getMonth()).slice(-2); day = ("0" + d).slice(-2); between.push(day + "." + month + "." + year); d++; } start.setMonth(start.getMonth() + 1); } console.log(between);