How about this?
function getDateArray (start, end) {
var arr = [];
var startDate = new Date(start);
var endDate = new Date(end);
while (startDate <= endDate) {
arr.push(new Date(startDate.setMonth(startDate.getMonth() + 1)));
}
return arr;
}
function test (){
var z = "2018-09-01"; var y = "2019-09-10"
var dateArr = getDateArray(z, y);
alert(dateArr[0] + "\n" + dateArr[1] + "\n" + dateArr[2]);
}
I changed your dt0 and endD to startDate and endDate.
Also the while loop is a bit easier and it seems to work.
You can see it run at:
JS Bin - Collaborative JavaScript Debugging[
^]
It pops up the first three dates in the array to show it works.
Looks like:
https://i.stack.imgur.com/n2nn3.png[
^]
After that I added the daysBetween() function:
function daysBetween ( date1, date2 )
{
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
return dayDifference = Math.ceil(timeDiff / (1000 * 3600 * 24));
}
Then I make the test() method call that too on two of the dates.
alert(daysBetween(dateArr[0], dateArr[3]))
Try it out at the jsbin.
You will see :
https://i.stack.imgur.com/awk1Q.png[
^]
Final Code
function calculateFinalArray(dateArr){
var dayCounts = "";
for (var x = 0; x < dateArr.length-1;x++)
{
dayCounts += daysBetween(dateArr[x],dateArr[x+1]) + ":";
}
alert(dayCounts);
}
Looks like this: (semicolon delimited list you can alter):
https://i.stack.imgur.com/tSm0H.png[
^]