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);
5
  • Possible duplicate of Difference between dates in JavaScript Commented Oct 17, 2018 at 21:06
  • Your example dates are confusing because they don't follow what you said. You have some dates that are more than a year apart. Commented Oct 17, 2018 at 21:08
  • @Herohtar How are they a year apart? It's only going up by 3 months. Commented Oct 18, 2018 at 13:11
  • Take another look. You have 8/19/2018 - 9/18/2019, etc. The years are different. Commented Oct 18, 2018 at 14:23
  • @Herohtar Oh thanks. i just fixed that. Commented Oct 18, 2018 at 14:24

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

13 Comments

The first number in that array should be 30 instead of 31 because the date is going from 19th to 18th.
@ace23 updated answer. Let me know if this helps you.
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 Could you give an example of what you tried? What do you mean "look at the date range"?
date range = 8/19/2018 , 9/19/2018,10/19/2019...I tried adding if else condition to this but the code crashes.
|
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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.