Get the Max/Min Date in Array of Objects in JavaScript


Borislav Hadzhiev

Last updated: Jul 25, 2022


Photo from Unsplash

Get the Max/Min Date in Array of Objects in JavaScript #

To get the Max/Min date in an array of objects:

  1. Use the map() method to get an array of Date objects.
  2. Unpack the values from the array in a call to the Math.max() or Math.min() functions.
  3. Pass the result to the Date() constructor.
const arr = [ {date: '2022-11-14T04:55:31.820Z'}, {date: '2022-09-24T07:25:31.820Z'}, {date: '2025-07-17T07:25:31.820Z'}, ]; // ✅ Get Max date const maxDate = new Date( Math.max( => { return new Date(; }), ), ); // 👇️ Thu Jul 17 2022 console.log(maxDate); // ✅ Get Min date const minDate = new Date( Math.min( => { return new Date(; }), ), ); // 👇️ Sat Sep 24 2022 console.log(minDate);

We have an array of objects where each object has a date property that points to a date and time string.

The date strings are formatted as ISO 8601 in the example, but this could be any other valid string that the Date() constructor is able to parse.

If your use case stores Date objects for the date property and not date strings, then you don't have to pass the value to the Date() constructor like we did in the map() method.

The function we passed to the method gets called with each element (object) in the array.

On each iteration, we pass the date string to the Date() constructor to create a Date object.

At this point we have an array of dates that we can unpack in a call to the Math.max or Math.min functions.

The Math.max and Math.min functions take multiple, comma-separated numbers and return the max or min value, depending on which function you call.

const arr = [5, 10, 15, 3, 6, 9]; const min = Math.min(...arr); console.log(min); // 👉️ 3 const max = Math.max(...arr); console.log(max); // 👉️ 15

We used the spread syntax (...), because the Math.max and Math.min functions expect multiple, comma-separated numbers and cannot directly get called with an array.

This works because under the hood each Date object stores a timestamp (the number of milliseconds since the Unix Epoch).

// 👇️ 1643013648670 console.log(new Date().getTime());

If you try to compare two Date objects, they get converted to timestamps before the comparison takes place.

// 👇️ 1668401731820 console.log( Math.max( ...[ new Date('2022-11-14T04:55:31.820Z'), new Date('2022-09-24T07:25:31.820Z'), ], ), );

The example above prints the timestamp of the latest date in the array.

If you need to convert this timestamp to a Date object, pass the timestamp as a parameter to the Date() constructor.

// 👇️ Mon Nov 14 2022 console.log( new Date( Math.max( ...[ new Date('2022-11-14T04:55:31.820Z'), new Date('2022-09-24T07:25:31.820Z'), ], ), ), );

The Date() constructor is quite flexible when it comes to parsing dates.

The examples above use it to create a Date object from a date string and from a timestamp.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.