Click here to Skip to main content
14,920,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 dates: StartDate and EndDate

With the help of the following code, i am able to create an array of months between 2 dates. 

Result = [10/09/2018, 11/09/2018, 12/09/2018, 01/09/2019...08/09/2019]

How do i calculate the number of days in each of those months and then create an array. What i am trying to do is calculate the number of days between those months.(depending on number of days)

Oct-Nov - 30 days
Nov-Dec - 31 days
Dec- Jan - 30 days
July- Aug - 30 days 
and so forth (depending on number of days)
and then create an array from that.

array = [30,30,31,30,28....] 

I also have a function that calculates number of days in each month depending on leap year.
<pre lang="text"><pre lang="Javascript">
function isLeapYear(year) {
       return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
   function getDaysInMonth(year, month) {
       return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];

Any help will be greatly appreciated. Thank you.

What I have tried:

var getDateArray = function(start, end) {
        var arr = [];
        var dt0 = new Date(start);
        var endD = new Date(end);
        while (dt0 <= endD) {
            arr.push(new Date(dt0));
            dt0.setMonth(dt0.getMonth() + 1);
        return arr;

     var z = "10/09/2018"; var y = "08/08/2019"
     var dateArr = getDateArray(z, y);
Updated 9-Oct-18 8:27am

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:[^]

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 :[^]

Final Code
function calculateFinalArray(dateArr){
  var dayCounts = "";
   for (var x = 0; x < dateArr.length-1;x++)
       dayCounts += daysBetween(dateArr[x],dateArr[x+1]) + ":";

Looks like this: (semicolon delimited list you can alter):[^]
Member 14013562 9-Oct-18 14:04pm
Thanks for replying. I think you misunderstood my question. I am already creating an array of dates. I am trying to find the number of days between each of those months. Lets say, there are 30 days between October to November, 30 between November to December and so forth. I need to create an array with the number of days. Ex: [30,30,31...and so forth]
raddevus 9-Oct-18 14:11pm
i updated it with the addt'l code. Tried to get it done before comment but was late. :)
Member 14013562 9-Oct-18 14:16pm
Yes. But, this will only get the total number of days between startdate and enddate. What i want need to show is how we get that total. So instead of doing 93 days total, i need an array with dates between each of those months. [31,30,31 and so forth].
I have a function that calculates that. I just edited my code. Not sure how to use that with this code.
raddevus 9-Oct-18 14:23pm
Okay, easy enough, I updated the script -- updated my entry to show the code. You can go to the jsbin and see the final code. You'll need to make sure you close the tab you had it previously loaded on and then open the link in a new tab in your browser to insure a refresh.
Member 14013562 9-Oct-18 14:53pm
It worked. Thank you so much! :)
When i try to calculate days between 10/8/2018 to 11/8/2019 it says 0. Also the array is short everytime by 1 count. Why does it do that?
You can use the difference between the two dates divided by the number of milliseconds in a day. Subtracting one from the other you can calculate the number of days between the two as follows:
function myFunction() {
    var startValue = new Date(2018,9,9,0,0,0,0);
    var endValue = new Date(2018,10,9,0,0,0,0);
    var days = (endValue - startValue) / (1000 * 60 * 60 * 24);

Note that the months are in the range 0 to 11, rather than 1 to 12. So the above calculates the number of days between 9th October and 9th November.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900