Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Sort array based on min and max date in JavaScript?
When working with arrays of date strings, you often need to find the oldest (minimum) and newest (maximum) dates. JavaScript provides several approaches to accomplish this task efficiently.
const arr = [ "2017-01-22 00:21:17.0", "2017-01-27 11:30:23.0", "2017-01-24 15:53:21.0", "2017-01-27 11:34:18.0", "2017-01-26 16:55:48.0", "2017-01-22 11:57:12.0", "2017-01-27 11:35:43.0" ];
We need to write a JavaScript function that takes such an array, finds the oldest and newest date, and returns an object containing both dates.
Using Array.reduce() Method
The reduce() method provides an elegant solution by iterating through the array and keeping track of minimum and maximum values:
const arr = [
"2017-01-22 00:21:17.0",
"2017-01-27 11:30:23.0",
"2017-01-24 15:53:21.0",
"2017-01-27 11:34:18.0",
"2017-01-26 16:55:48.0",
"2017-01-22 11:57:12.0",
"2017-01-27 11:35:43.0"
];
const findMinMaxDate = (arr = []) => {
const res = arr.reduce((acc, val, ind) => {
if (!ind) {
return {
min: val,
max: val
};
}
if (val < acc.min) {
acc.min = val;
}
if (val > acc.max) {
acc.max = val;
}
return acc;
}, undefined);
return res;
};
console.log(findMinMaxDate(arr));
{ min: '2017-01-22 00:21:17.0', max: '2017-01-27 11:35:43.0' }
Using Math.min() and Math.max() with Spread Operator
For a more concise approach, you can use Math.min() and Math.max() with the spread operator:
const findMinMaxDateConcise = (arr = []) => {
return {
min: Math.min(...arr),
max: Math.max(...arr)
};
};
console.log(findMinMaxDateConcise(arr));
{ min: '2017-01-22 00:21:17.0', max: '2017-01-27 11:35:43.0' }
Using Date Objects for Proper Date Comparison
For more robust date handling, convert strings to Date objects before comparison:
const findMinMaxDateObject = (arr = []) => {
const dates = arr.map(dateStr => new Date(dateStr));
const minDate = new Date(Math.min(...dates));
const maxDate = new Date(Math.max(...dates));
return {
min: minDate.toISOString().replace('T', ' ').slice(0, -1) + '0',
max: maxDate.toISOString().replace('T', ' ').slice(0, -1) + '0'
};
};
console.log(findMinMaxDateObject(arr));
{ min: '2017-01-22 00:21:17.0', max: '2017-01-27 11:35:43.0' }
Comparison
| Method | Performance | Readability | Date Safety |
|---|---|---|---|
| Array.reduce() | Good | Medium | String comparison only |
| Math.min/max() | Best | High | String comparison only |
| Date Objects | Medium | Medium | Proper date handling |
Key Points
- String comparison works well for ISO date formats due to lexicographical ordering
- The
Math.min/max()approach is most concise for simple cases - Use Date objects when working with different date formats or need date arithmetic
- Always handle empty arrays by providing default parameters
Conclusion
For ISO date strings, the Math.min/max() approach provides the cleanest solution. Use Date objects when you need robust date handling across different formats or time zones.
