Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to store all dates in an array present in between given two dates in JavaScript ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given two dates and the task is to get the array of dates between the two given dates using JavaScript. 

Approach 1:

  • Select the first and last date and store it in a variable.
  • Check if the start date is less than the stop date then push the current date in an array and increment its value by 1 day.
  • Repeat the above step until currentDate equal to the last date.

Example: In this example, the array of dates is determined by the above approach. 

html




<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p id="GFG_UP" style="font-size: 19px; font-weight: bold;">
    </p>
  
    <button onclick="GFG_Fun()">
        click here
    </button>
  
    <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;">
    </p>
  
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
              
        el_up.innerHTML = "Click on the button to get the "
                    + "array of dates between 2 dates.";
          
        Date.prototype.addDay = function(days) {
            var date = new Date(this.valueOf());
            date.setDate(date.getDate() + days);
            return date;
        }
          
        function getDate(strDate, stpDate) {
            var dArray = new Array();
            var cDate = strDate;
            while (cDate <= stpDate) {
                  
                // Adding the date to array
                dArray.push(new Date (cDate) + '<br>');
                  
                // Increment the date by 1 day
                cDate = cDate.addDay(1);
            }
            return dArray;
        }
          
        function GFG_Fun() {
            var startDate = new Date();
              
            // Making lastDate equal to 4 more days
            // from startDate.
            var endDate = startDate.addDay(4);
            el_down.innerHTML = getDate(startDate, endDate);
        }
    </script>
</body>

Output:

 

Approach 2:

  • Get the first and last date and store it into a variable.
  • Calculate 1 day equivalent in milliseconds called _1Day.
  • Set a variable equal to the start date, called ms
  • Push ms (milli-seconds) in form of a date in an array and increment its value by _1Day.
  • Repeat the above step until ms is equal to the last date.

Example: In this example, the array of dates is determined by the above approach. 

html




<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p id="GFG_UP" style="font-size: 19px; font-weight: bold;">
    </p>
  
    <button onclick="GFG_Fun()">
        click here
    </button>
  
    <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;">
    </p>
  
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
          
        el_up.innerHTML = "Click on the button to get the "
                + "array of dates between 2 dates.";
          
        Date.prototype.addDay = function(days) {
            var date = new Date(this.valueOf());
            date.setDate(date.getDate() + days);
            return date;
        }
          
        function getDates( date1, date2 ) {
            var _1Day = 24*3600*1000;
              
            // Date[] keeps all the dates
            for (var date = [], ms = date1 * 1, last = date2 * 1;
                            ms < last ; ms += _1Day) {
                                  
                // Adding ms to the date and ms+= _1Day
                // increments the date by 1 day
                date.push( new Date(ms) + '<br>');
            }
              
            return date;
        }
          
        function GFG_Fun() {
            var startDate = new Date();
              
            // Making lastDate equal to 4 more days
            // from startDate
            var endDate = startDate.addDay(4);
            el_down.innerHTML = getDates(startDate, endDate);
        }
    </script>
</body>

Output:

 


My Personal Notes arrow_drop_up
Last Updated : 20 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials