Friday, October 17, 2025
HomeLanguagesJavascriptHow to select Min/Max dates in an array using JavaScript ?

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

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

Below are the following approaches:

Approach 1: Use Math.max.apply() and Math.min.apply() Methods

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

Javascript




let 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"));
 
function GFG_Fun() {
    let maximumDate = new Date(Math.max.apply(null, dates));
    let minimumDate = new Date(Math.min.apply(null, dates));
 
    console.log("Max date is - " + maximumDate);
    console.log("Min date is - " + minimumDate);
}
GFG_Fun();


Output

Max date is - Fri Jun 28 2019 00:00:00 GMT+0000 (Coordinated Universal Time)
Min date is - Tue Jun 25 2019 00:00:00 GMT+0000 (Coordinated Universal Time)

Approach 2: Use reduce() method

  • 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. 

Javascript




let 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"));
 
function GFG_Fun() {
    let mnDate = dates.reduce(function (a, b) {
        return a < b ? a : b;
    });
 
    let mxDate = dates.reduce(function (a, b) {
        return a > b ? a : b;
    });
 
    console.log("Max date is - " + mxDate);
    console.log("Min date is - " + mnDate);
}
GFG_Fun();


Output

Max date is - Fri Jun 28 2019 00:00:00 GMT+0000 (Coordinated Universal Time)
Min date is - Tue Jun 25 2019 00:00:00 GMT+0000 (Coordinated Universal Time)
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS