Given an array of JavaScript date. The task is to get the minimum and maximum date of the array using JavaScript.
Approach 1:
- Get the JavaScript dates in an array.
- Use Math.max.apply() and Math.min.apply() function to get the maximum and minimum dates respectively.
Example: In this example, the maximum and minimum date is determined by the above approach.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Min/Max of dates in an array. </title> </head> <body style = "text-align:center;" id = "body"> <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"); 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> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

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.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Min/Max of dates in an array. </title> </head> <body style = "text-align:center;" id = "body"> <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"); 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> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- How to store all dates in an array present in between given two dates in JavaScript ?
- Return all dates between two dates in an array in PHP
- Compare two dates using JavaScript
- How to calculate minutes between two dates in JavaScript ?
- How to calculate the number of days between two dates in javascript?
- How to check if one date is between two dates in JavaScript ?
- Get the relative timestamp difference between dates in JavaScript
- Sort an array of dates in PHP
- How to set the value of a select box element using JavaScript?
- How to select all text in HTML text input when clicked using JavaScript?
- How to use Checkbox inside Select Option using JavaScript ?
- How to select a random element from array in JavaScript ?
- Program to find the number of days between two dates in PHP
- How to calculate the difference between two dates in PHP?
- Comparing two dates in PHP
- How to get highcharts dates in the x-axis ?
- How to select all child elements recursively using CSS?
- How to select elements by data attribute using CSS?
- How to select text input fields using CSS selector ?
- How to select all children of an element except the last child using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


