Get The Max/Min Date From An Array Of Objects In JavaScript

To get the max/min date from an array of objects in JavaScript –

  1. Call the map() method to get an array of Date objects.
  2. For each Date object, pass the date to the Date() constructor to get a timestamp.
  3. Find the max/min timestamp with Math.max() and Math.min().

Let’s discuss this method in detail below.

get the min/max date from an array of objects in JavaScript

Get The Max/Min Date From An Array Of Objects In JavaScript

We can use the map() method to get an array of Date objects from an array of dates.

For each Date object, we call the Date() constructor to get a timestamp.

Then, we find the max/min timestamp with Math.max() and Math.min().

Here’s the code:

const dates = [{date: '2018-01-01'}, {date: '2019-02-02'}, {date: '2020-03-03'}];

const getMaxDate = arr => {

 const timestamps = arr.map(({ date }) => new Date(date));

 return Math.max(…timestamps);

};

const getMinDate = arr => {

 const timestamps = arr.map(({ date }) => new Date(date));

 return Math.min(…timestamps);

};

console.log(new Date(getMaxDate(dates)).toLocaleString()); // "3/3/2020, 5:30:00 AM"

console.log(new Date(getMinDate(dates)).toLocaleString()); // "1/1/2018, 5:30:00 AM"

As you can see from the code above, we first created an array of date objects.

Then, we passed each object to the Date() constructor to get a timestamp.

After that, we used Math.max() and Math.min() to find the max/min timestamp.

Finally, we converted the timestamps back to Date objects with the new Date() constructor.

You can also use the sort() method to get the max/min date.

First, convert the dates to timestamps with the Date() constructor.

Then, sort the timestamps in ascending or descending order and return the first or last element respectively.

Here’s the code:

const dates = [{date: '2018-01-01'}, {date: '2019-02-02'}, {date: '2020-03-03'}];

const getMaxDate = arr => {

 const timestamps = arr.map(({ date }) => new Date(date));

 return timestamps.sort((a, b) => b – a)[0]; // sort in descending order and return the first element

};

const getMinDate = arr => {

 const timestamps = arr.map(({ date }) => new Date(date));

 return timestamps.sort((a, b) => a – b)[0]; // sort in ascending order and return the first element

};

console.log(new Date(getMaxDate(dates)).toLocaleString()); // "3/3/2020, 5:30:00 AM"

console.log(new Date(getMinDate(dates)).toLocaleString()); // "1/1/2018, 5:30:00 AM"

As you can see from the code above, we first converted the dates to timestamps with the Date() constructor.

Then, we sorted the timestamps in descending or ascending order and returned the first element.

You can also use the reduce() method to get the max/min date.

The reduce() method executes a callback function for each element of an array and accumulates the result in a single value.

First, convert the dates to timestamps with the Date() constructor.

Then, use the reduce() method to find the max/min timestamp.

Here’s the code:

const dates = ['2018-01-01', '2019-02-02', '2020-03-03'];

const getMaxDate = arr => arr.reduce((max, date) => Math.max(max, new Date(date)), -Infinity);

const getMinDate = arr => arr.reduce((min, date) => Math.min(min, new Date(date)), Infinity);

console.log(new Date(getMaxDate(dates)).toLocaleString()); // "3/3/2020, 5:30:00 AM"

console.log(new Date(getMinDate(dates)).toLocaleString()); // "1/1/2018, 5:30:00 AM"

As you can see from the code above, we first converted the dates to timestamps with the Date() constructor.

Then, we used the reduce() method to find the max/min timestamp.

Conclusion – Get The Max/Min Date From An Array Of Objects In JavaScript

In this article, we showed you three ways to get the max/min date from an array of objects in JavaScript.

We hope you find this article helpful. Thanks for reading!

Leave a Reply