0

I have 2 dates: startdate and enddate. End date is always a day less than the startdate. So if my start day is 19th, the end date would be on the 18th of next month.

I am trying to create an array of number of days in between the 2 dates. (It goes from 19th to 18th and then 18th to 18th of every month to calculate the difference)

Example

8/19/2018 - 9/18/2018 = 30 days

9/18/2018 - 10/18/2019 = 30 days

10/18/2018 - 11/18/2018 = 31 days

array = [30,30,31]

I am using the following code to calculate days difference between the dates.

  function daysBetweenArrears (date1, date2){

         date1.setDate(date1.getDate() );
         date2.setDate(date2.getDate() - 1);
         var Diff = Math.abs(date2.getTime() - date1.getTime());
         var TimeDifference = Math.round(Diff / (1000 * 3600 * 24));
         return TimeDifference;
    }

The following code for creating the array

  if (document.getElementById("endDate"))
             y = document.getElementById("endDate").value;
          if (document.getElementById("startDate"))
             z = document.getElementById("startDate").value;

var dateArr = getDateArray(z, y);
     var dayCountArr = "";
     var b = [];

     for (var x = 0; x < dateArr.length-1; x++)
     {
         dayCountArr += daysBetweenArrears(dateArr[x], dateArr[x+1], ",");
         b.push(daysBetweenArrears(dateArr[x], dateArr[x+1]));
     }

The issue is that when i set the date as following, it is giving me incorrect output. The problem is that it is setting the dates incorrectly whenever it goes to the next month. I am not sure what i am doing wrong here. Any help is greatly appreciated. Thank you.

date2.setDate(date2.getDate() - 1);
ace23
  • 142
  • 1
  • 16

2 Answers2

1

You can do this using moment. Hope this helps.

const start = "8/19/2018";
const end = "11/18/2018 ";

const dates = [];

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

for (let i = 0; mstart < mend ; i++) {

    const daysInMonth = mstart.daysInMonth() + (i === 0 ? -1 : 0);

    dates.push(daysInMonth);

    mstart.add(1, 'M');
}

console.log(dates);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Alex G
  • 1,897
  • 2
  • 10
  • 15
  • The first number in that array should be 30 instead of 31 because the date is going from 19th to 18th. – ace23 Oct 18 '18 at 15:00
  • @ace23 updated answer. Let me know if this helps you. – Alex G Oct 19 '18 at 07:26
  • I tried this. I t works but for some reason it crashes if i try to look at the date range and i am also not able to add any if else statements! – ace23 Nov 02 '18 at 16:24
  • @ace23 Could you give an example of what you tried? What do you mean "look at the date range"? – Alex G Nov 02 '18 at 16:44
  • date range = 8/19/2018 , 9/19/2018,10/19/2019...I tried adding if else condition to this but the code crashes. – ace23 Nov 02 '18 at 17:38
  • I tried this. const range = moment.range(mstart, mend); – ace23 Nov 02 '18 at 17:41
  • @ace23 range isn't by default in moment. See this https://momentjs.com/docs/#/plugins/range/ and this https://github.com/rotaready/moment-range. Basically you have to extend moment with moment-range to use this. – Alex G Nov 02 '18 at 17:44
  • @ace23 also is still don't get what you mean about the if else condition. Please give an example if you can. – Alex G Nov 02 '18 at 17:45
  • (i === 0 ? -1 : 0); What exactly is this doing? I am confused. – ace23 Nov 02 '18 at 19:50
  • @ace23 Since you want your dates to be from X to X - 1 of the next month, I remove 1 day from the first range. – Alex G Nov 02 '18 at 21:27
  • Thank you for your help. One more question! If i have a payment due on 19th of every month but here my end date is on the 18th so, if the date range is not a complete month, i need to reduce the array by 1 month. In that case i want my array will only print [30,30]. How do i do that? – ace23 Nov 05 '18 at 21:47
  • @ace23 Well if you want to remove one month from the array if the days of the start and end months are not equal, you can do this after the loop: if (new Date(start).getDate() !== new Date(end).getDate()) dates.pop(); – Alex G Nov 06 '18 at 08:47
  • Great. Thank you Alex. – ace23 Nov 06 '18 at 14:24
0

You can update your function daysBetweenArrears

const daysBetweenArrears = (date1, date2) => {
  const time1 = new Date(date1).getTime();
  const time2 = new Date(date2).getTime();
  const diff = Math.abs(time2 - time1);
  return Math.round(diff/(1000*60*60*24));
};

console.log(daysBetweenArrears('8/18/2018', '9/18/2018'));
console.log(daysBetweenArrears('6/18/2018', '7/18/2018'));
Pengcheng
  • 323
  • 1
  • 5