How to useMath.max.apply() and Math.min.apply() Methods in Javascript

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)

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:

Table of Content

  • Using Math.max.apply() and Math.min.apply() Methods
  • Using reduce() method
  • Using Spread Operator
  • Using Array.prototype.sort with a custom comparator

Similar Reads

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

Get the JavaScript dates in an array.Use Math.max.apply() and Math.min.apply() function to get the maximum and minimum dates respectively....

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

Approach 3: Using Spread Operator

To select the minimum and maximum dates in an array, use the spread operator to clone the array, then apply `Math.min` and Math.max with the spreaded array, respectively....

Approach 4: Using Array.prototype.sort with a custom comparator

Using Array.prototype.sort with a custom comparator function sorts an array of dates in ascending order. The first element (`dates[0]`) is the minimum date, and the last element (`dates[dates.length – 1]`) is the maximum date, efficiently determining the min/max dates in the array....