The Wayback Machine - https://web.archive.org/web/20240121200545/https://www.geeksforgeeks.org/how-to-select-min-max-dates-in-an-array-using-javascript/
Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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: Using 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: Using 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)

Approach 3: Using Spread Operator

Example: In this example, we are using sort() Method.

Javascript




let dates = [new Date('2022-01-01'), new Date('2022-03-15'), new Date('2022-02-10')];
let minDate = new Date(Math.min(...dates));
let maxDate = new Date(Math.max(...dates));
console.log(minDate);
console.log(maxDate);


Output

2022-01-01T00:00:00.000Z
2022-03-15T00:00:00.000Z

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 08 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials