Get the Min/Max Dates in an Array using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 24, 2022

banner

Photo from Unsplash

Get the Min/Max Dates in an Array using 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

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 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 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.