Solutions To Get The Min/Max Dates In An Array In JavaScript?

Get the Min/Max Dates in an Array in JavaScript

Array and Date are two widespread data types in JavaScript. If you know how to manipulate the built-in functions in JS, it will be less difficult to work with them. So in this article, we will show you how to get the Min/Max Dates in an Array in JavaScript using built-in functions.

Get the Min/Max Dates in an array in JavaScript

We will show you three simple ways to quickly get get the Max/Min Date in an array in JavaScript:

Using forEach() function

We introduced the forEach() syntax in a previous post. You can read more here.

This way, we will manually create two functions, maxDate() and minDate(). The algorithm to create the function maxDate() is as follows: first, we assign maxDate as the first element of the dates array. Next, we loop through the entire array and check if the Date in the current loop is greater than maxDate, then set maxDate equal to that Date.

The algorithm of the minDate() is the same. We assign minDate as the first element in the array and then iterate through the dates in the array. If the Date in the current loop is less than minDate, then we set minDate equal to that Date. Look closely at the example below to understand the two algorithms better.

let dates = ['2021/10/01', '2022/12/31', '2023/01/01'];

function maxDate(dates) {
  let maxDateObj = new Date(dates[0]);

  // Using forEach()
  dates.forEach(function (date) {
    if (new Date(date) > maxDateObj) {
      maxDateObj = new Date(date);
    }
  });
  return maxDateObj;
}

function minDate(dates) {
  let minDateObj = new Date(dates[0]);

  // Using forEach()
  dates.forEach(function (date) {
    if (new Date(date) < minDateObj) {
      minDateObj = new Date(date);
    }
  });
  return minDateObj;
}

console.log('Max Date: ' + maxDate(dates).toLocaleDateString());
console.log('Min Date: ' + minDate(dates).toLocaleDateString());

Output:

Max Date: 1/1/2023
Min Date: 10/1/2021

Using Math.min() function and Math.max() function

The Math.min() method will return the number with the smallest value in the set of numbers.

The Math.max() method will return the number with the largest value in the set of numbers.

Math.min() syntax:

Math.min(n1, n2, n3, ...)

Math.max() syntax:

Math.max(n1, n2, n3, ...)

Parameters:

  • n1,n2,n3, … are numeric values.

First, we will push all date objects initialized from the value of the element in the dateArray to an empty array, then use the spread operator to pass the values in the array to Math.max() and Math.min() to get the Max/Min Date in an array in JavaScript. Like this:

let dateArray = ['2021/10/01', '2022/12/31', '2023/01/01'];
let dates = [];

for (let date of dateArray) {
  dates.push(new Date(date));
}

// Using Math.max()
let maxDate = new Date(Math.max(...dates));

// Using Math.min()
let minDate = new Date(Math.min(...dates));
console.log('Max Date: ' + maxDate.toLocaleDateString());
console.log('Min Date: ' + minDate.toLocaleDateString());

Output:

Max Date: 1/1/2023
Min Date: 10/1/2021

Using reduce() function

We introduced the reduce() syntax in a previous post. You can read more here if you want.

In this case, to find the min date, we use the reduce() function to iterate through all the elements in the array of dates and compare. If the new Date of the current element is less than the new Date of the next element, then we will return the current element; otherwise, we will return the next element. The same is true for the max Date. If the new Date of the current element is greater than the new Date of the next element, we return the current element; otherwise, we return the next element. Look closely at the example below to see how to use the reduce() function.

let dates = ['2021/10/01', '2022/12/31', '2023/01/01'];

// Using reduce()
const minDate = dates.reduce((acc, date) => {

  // If acc < date then acc = date in next loop
  return new Date(acc) < new Date(date) ? acc : date;
});
const maxDate = dates.reduce((acc, date) => {

  // If acc > date then acc = date in next loop
  return new Date(acc) > new Date(date) ? acc : date;
});

console.log('Max Date: ' + new Date(maxDate).toLocaleDateString());
console.log('Min Date: ' + new Date(minDate).toLocaleDateString());

Output:

Max Date: 1/1/2023
Min Date: 10/1/2021

Summary

This post is also quite long. We hope you got all the knowledge from this article about how to get the Min/Max Dates in an array in JavaScript. If you have any problems with the methods above. Please leave a comment below.

Have a lucky day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *