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

avatar
Borislav Hadzhiev

Last updated: Jan 13, 2023
4 min

banner

# Table of Contents

  1. Get the Max/Min Date in an Array of Objects in JavaScript
  2. Get the Min/Max Dates in an Array in JavaScript

# Get the Max/Min Date in an 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.
index.js
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( ...arr.map(element => { return new Date(element.date); }), ), ); // ๐Ÿ‘‡๏ธ Thu Jul 17 2022 console.log(maxDate); // โœ… Get Min date const minDate = new Date( Math.min( ...arr.map(element => { return new Date(element.date); }), ), ); // ๐Ÿ‘‡๏ธ Sat Sep 24 2022 console.log(minDate);

pushing an object to an array using array concat

If you need to get the Min/Max dates in a flat array, scroll down to the next subheading.

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 Array.map() 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.

index.js
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).

index.js
// ๐Ÿ‘‡๏ธ 1643013648670 console.log(new Date().getTime());

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

index.js
// ๐Ÿ‘‡๏ธ 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.

index.js
// ๐Ÿ‘‡๏ธ 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.

# Get the Min/Max Dates in an Array in JavaScript

To get the Min/Max dates in an array:

  1. Unpack the arrays in a call to the Math.min() or Math.max() functions.
  2. Pass the result to the Date() constructor.
  3. The Math.min and Math.max functions will return the timestamp of the Min/Max date.
index.js
const arr = [ new Date('2022-11-14'), new Date('2022-09-24'), new Date('2025-07-17'), ]; const min = new Date(Math.min(...arr)); console.log(min); // ๐Ÿ‘‰๏ธ Sat Sep 24 2022 const max = new Date(Math.max(...arr)); console.log(max); // ๐Ÿ‘‰๏ธ Thu Jul 17 2025

get min max dates in array

We have an array of Date objects in the example. If you have an array of date strings, you can convert each date string to a Date object by passing it to the Date() constructor.

index.js
const arrStrings = ['2022-11-14', '2022-09-24', '2025-07-17']; // ๐Ÿ‘‡๏ธ convert date strings to date objects const arrDates = arrStrings.map(str => new Date(str)); const min = new Date(Math.min(...arrDates)); console.log(min); // ๐Ÿ‘‰๏ธ Sat Sep 24 2022 const max = new Date(Math.max(...arrDates)); console.log(max); // ๐Ÿ‘‰๏ธ Thu Jul 17 2025

The example above uses the same approach as the first code snippet but has a starting point of an array containing date strings and not Date objects.

We used the Math.max() and Math.min functions to get the min and max dates in the array.

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.
index.js
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, which means we can't directly pass an array.

This works because each date stores a timestamp under the hood. A timestamp, as in the number of milliseconds elapsed between midnight of the 1st of January 1970 and the given date.
index.js
// ๐Ÿ‘‡๏ธ 1643015842501 console.log(new Date().getTime());

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

index.js
const arr = [ new Date('2022-11-14'), new Date('2022-09-24'), new Date('2025-07-17'), ]; // ๐Ÿ‘‡๏ธ 1752710400000 console.log(Math.max(...arr));

If you need to convert this timestamp to a Date object, so you can take advantage of some of the built-in functions, you can pass the timestamp as a parameter to the Date() constructor.

index.js
const arr = [ new Date('2022-11-14'), new Date('2022-09-24'), new Date('2025-07-17'), ]; // ๐Ÿ‘‡๏ธ Thu Jul 17 2022 console.log(new Date(Math.max(...arr)));

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

The example above uses it to create a Date object from a date string and a timestamp.

I've also written an article on how to sort an array of objects by Date property.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2023 Borislav Hadzhiev