How to useMath.max.apply() and Math.min.apply() Methods in Javascript
- 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.
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