Q:

Get an array with number of days

I am trying to create an array with number of days between 2 dates.
The dates can be anything but i will be using the following:
Start : 11/30/2018, End: 09/30/2019
Array= [30,31,29,30....31]

What i am trying to achieve:

What i have tried:

const start = "11/30/2018";
const end = "09/30/2019";

const dates = [];

const mstart = moment(new Date(start));
const mend = moment(new Date(end));

let i = 0;
while (1 == 1) {
  let nextStart = mstart.clone().add(i, 'M');
  let nextEnd = mstart.clone().add(i + 1, 'M') > mend ? mend : mstart.clone().add(i + 1, 'M');

  dates.push(nextEnd.diff(nextStart, 'days'));

  if (nextEnd >= mend) {
    break;
  }

  i += 1
}


console.log(dates);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

The date array ranges from 30-30 in the first few columns and 30-29 in last columns except the leap year. 
The first case works. How do i make the date range from 30-29th for the second case? I tried .subtract(1, 'days') but it doesn't return the right result because i want the date range to work too instead of just subtracting a day every time.
Any help is appreciated. Thank you.

A:

To achieve expected result, use below option 

Do another check for end date checking if greater than 28
Add and subtract days based on that condition

const start = "11/30/2018";
const end = "09/30/2019";

const dates = [];

const mstart = moment(new Date(start));
const mend = moment(new Date(end));
let nextEnd

let i = 0;
while (1 == 1) {
  let nextStart = nextEnd? (nextEnd.date() > 28 ? nextEnd.add(1, 'days') : nextEnd) : nextEnd || mstart.clone().add(i, 'M');
  nextEnd = mstart.clone().add(i + 1, 'M') > mend ? mend : mstart.clone().add(i +1, 'M');

  if(nextEnd.date() > 28){
    nextEnd.subtract(1, 'days')
  }else{
   
  }
  console.log("start-->", nextStart.format('MM/DD/YYYY'), "end--", nextEnd.format('MM/DD/YYYY'))

 

  if (nextEnd >= mend) {
    break;
  }else{
     dates.push(nextEnd.diff(nextStart, 'days'));
  }

  i += 1
}


console.log(dates)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

codepen - https://codepen.io/nagasai/pen/LXaKKY?editors=1010

