Skip to content
Related Articles
Open in App
Not now

Related Articles

How to select Min/Max dates in an array using JavaScript ?

Improve Article
Save Article
  • Last Updated : 20 Dec, 2022
Improve Article
Save Article

Given an array of JavaScript date. The task is to get the minimum and maximum date of the array using JavaScript. 

Approach 1:

Example: In this example, the maximum and minimum date is determined by the above approach. 

html




<body style="text-align:center;" id="body">
 
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>Select Min/Max dates in an array</h3>
 
    <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");
        var dates = [];
         
        dates.push(new Date("2019/06/25"));
        dates.push(new Date("2019/06/26"));
        dates.push(new Date("2019/06/27"));
        dates.push(new Date("2019/06/28"));
         
        el_up.innerHTML = dates[0] + "<br>" + dates[1]
                + "<br>" + dates[2] + "<br>" + dates[3];
         
        function GFG_Fun() {
            var maximumDate=new Date(Math.max.apply(null, dates));
            var minimumDate=new Date(Math.min.apply(null, dates));
             
            el_down.innerHTML = "Max date is - " + maximumDate
                    + "<br>Min date is - " + minimumDate;
        }
    </script>
</body>

Output:

 select Min/Max dates in an array 

Approach 2:

  • Get the JavaScript dates in an array.
  • Use reduce() method in an array of dates and define the respective function for the maximum and minimum dates.

Example: In this example, the maximum and minimum date is determined by the above approach. 

html




<body style="text-align:center;" id="body">
 
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>Select Min/Max dates in an array using reduce()</h3>
 
    <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");
        var dates = [];
         
        dates.push(new Date("2019/06/25"));
        dates.push(new Date("2019/06/26"));
        dates.push(new Date("2019/06/27"));
        dates.push(new Date("2019/06/28"));
         
        el_up.innerHTML = dates[0] + "<br>" + dates[1]
                + "<br>" + dates[2] + "<br>" + dates[3];
         
        function GFG_Fun() {
            var mnDate = dates.reduce(function (a, b) {
                return a < b ? a : b;
            });
             
            var mxDate = dates.reduce(function (a, b) {
                return a > b ? a : b;
            });
             
            el_down.innerHTML = "Max date is - " + mxDate
                        + "<br>Min date is - " + mnDate;
        }
    </script>
</body>

Output:

 select Min/Max dates in an array 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!