A clean way to do it would be to convert each date to a Date() and take the max

ES6:

new Date(Math.max(...a.map(e => new Date(e.MeasureDate))));

JS:

new Date(Math.max.apply(null, a.map(function(e) {
  return new Date(e.MeasureDate);
})));

where a is the array of objects.

What this does is map each of the objects in the array to a date created with the value of MeasureDate. This mapped array is then applied to the Math.max function to get the latest date and the result is converted to a date.

By mapping the string dates to JS Date objects, you end up using a solution like Min/Max of dates in an array?

--

A less clean solution would be to simply map the objects to the value of MeasureDate and sort the array of strings. This only works because of the particular date format you are using.

a.map(function(e) { return e.MeasureDate; }).sort().reverse()[0]

If performance is a concern, you may want to reduce the array to get the maximum instead of using sort and reverse.

Answer from arcyqwerty on Stack Overflow
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-date-exercise-11.php
JavaScript: Maximum date from an array of dates - w3resource
July 17, 2025 - It iterates through each date string in the 'all_dates' array using the "forEach()" method. ... It converts the current date string to a Date object. It compares the current Date object with the maximum Date object (max_dtObj).
Discussions

javascript - Get the minimum and maximum dates from JSON - Stack Overflow
As in JavaScript to get the maximum and minimum date of it? ... Either sort this array by dates and take first and last value (if array size is small), or iterate and keep comparing against smallest and largest value. More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Get max and min dates from an array - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Finding minimum and maximum date in an complex json array - Stack Overflow
I would prefer to use angular if not javascript or jquery is also fine. ... SO isn't a write code for me service. Please post what you have tried atleast. ... I am able to achieve the same by find the minimum and maximum dates for each object inside array. Like for Dummy1,Dummy2 and Dummy3 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 21, 2016
Finding the max date from an array of dates using javascript - Stack Overflow
There are two date fields namely 'Date 1' and 'Date 2'. On clicking the button 'Check', the maximum of the two dates need to be printed in the 'Max Date' field. So how to print the max date from a ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-get-max-min-date-in-array-of-objects
Get the Max/Min Date in an Array of Objects in JavaScript | bobbyhadz
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. ... Copied!// ๐Ÿ‘‡๏ธ 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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-select-min-max-dates-in-an-array-using-javascript
How to Select Min/Max Dates in an Array Using JavaScript? - GeeksforGeeks
July 12, 2025 - It uses Math.max() and Math.min() ... result is printed using console.log(). Use reduce() method in an array of dates and define the respective function for the maximum and minimum dates. JavaScript ยท...
Top answer
1 of 4
1

var array = [{"source":"2016-11-02","sourcecount":38},{"source":"2016-11-01","sourcecount":30},{"source":"2016-11-02","sourcecount":30},{"source":"2016-11-03","sourcecount":30}];

var max = null;
var min = null;

for (var i = 0; i < array.length; i++) {
  var current = array[i];
  if (max === null || current.source > max.source) {
    max = current;
  }
  if (min === null || current.source < min.source) {
    min = current;
  }
}

document.getElementById('maxResult').innerHTML = max.source;
document.getElementById('minResult').innerHTML = min.source;
Max: <span id="maxResult"></span><br/ >
Min: <span id="minResult"></span>

2 of 4
1

You could do something like this, provided your date format is "yyyy-MM-dd".

Convert the date string to dateKey. which always follow the ascending order as the dates proceed. 20160101(Jan 1st) is always less than 20161231(Dec 31st).

Keeping that in mind, just convert the dates to dateKey and map dateKeys to the object and just extract the max and min of the dateKeys and return the actual date.

var datesArray = [{
  "source": "2016-11-02",
  "sourcecount": 38
}, {
  "source": "2016-11-10",
  "sourcecount": 30
}, {
  "source": "2016-11-31",
  "sourcecount": 38
}, {
  "source": "2016-01-01",
  "sourcecount": 30
}];

var newObject = {};
var dates = datesArray.map(function(obj) {
  var regEx = new RegExp(/-/g);
  //Convert date to dateKey
  var dateKey = parseInt(obj.source.replace(regEx, ""), 10)
  newObject[dateKey] = obj;
  return dateKey;
});

console.log("Max", newObject[Math.max(...dates)].source);
console.log("Min", newObject[Math.min(...dates)].source);

๐ŸŒ
Medium
medium.com โ€บ @luke.sloanebulger โ€บ how-to-find-the-min-and-max-dates-in-a-javascript-array-baa6dd5deb66
How To Find The Min and Max Dates in a JavaScript Array? | by Luke Sloane-Bulger | Medium
November 3, 2023 - To get the min and max date from an array of dates in JavaScript, we can use the Math.min() and Math.max() static methods. Then using the spread operator ... on the array to pass each date as an argument in the Math.min() and Math.max() converts ...
Find elsewhere
๐ŸŒ
CodeVsColor
codevscolor.com โ€บ find out the largest date in a javascript array - codevscolor
Find out the largest Date in a JavaScript array - CodeVsColor
February 6, 2020 - Math.max.apply can be used to find out the largest object in an array. It will return the largest value in milliseconds. So, you need to wrap it with Date constructor to get the Date object value. let dateArray = [] dateArray.push(new ...
๐ŸŒ
Plain English
plainenglish.io โ€บ home โ€บ blog โ€บ javascript โ€บ how to get the min and max dates in a javascript date array?
How to Get the Min and Max Dates in a JavaScript Date Array?
March 23, 2023 - We spread the dates array into Math.max and Math.min, which will convert the dates entries into UNIX timestamps, which are numbers.
Top answer
1 of 3
2

There is my solution, this is a bit long, but very easy to understand and edit for any update. I'm sure we can solve it with 4-5 lines, but whatever, it works.

First, define a month array :

var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

Like every time you need to search min/max in an array, you need to initialize min and max with the first value :

var max_end_date = data[0].Events[1].Date;
var min_start_date = data[0].Events[0].Date;

And then, split every date for compare year / month for start en end, and you have a result like this :

$.each(data, function(k, v) {
    var month_min_start_date = min_start_date.split("-")[0];
    var year_min_start_date = min_start_date.split("-")[1];

    var current_start_date = v.Events[0].Date;
    var month_current_start_date = current_start_date.split("-")[0];
    var year_current_start_date = current_start_date.split("-")[1];

    if (year_current_start_date < year_min_start_date 
        || year_current_start_date == year_min_start_date && month.indexOf(month_current_start_date) < month.indexOf(month_min_start_date)) {
            min_start_date = current_start_date;
        }

    var month_max_end_date = max_end_date.split("-")[0];
    var year_max_end_date = max_end_date.split("-")[1];

    var current_end_date = v.Events[1].Date;
    var month_current_end_date = current_end_date.split("-")[0];
    var year_current_end_date = current_end_date.split("-")[1];

    if (year_current_end_date > year_max_end_date 
        || year_current_end_date == year_max_end_date && month.indexOf(month_current_end_date) > month.indexOf(month_max_end_date)) {
            max_end_date = current_end_date;
        }

})

You can try it on JSFiddle

2 of 3
1

Here's another approach using Math

var data=[
{
    Title:"Dummy1",
    Events:[{
    eventName:"StartDate",
    Date:"Jan-2014"
    },
    {
    eventName:"EndDate",
    Date:"Feb-2015"
    }]
},
{
    Title:"Dummy2",
    Events:[{
    eventName:"StartDate",
    Date:"Jan-2013"
    },
    {
    eventName:"EndDate",
    Date:"Feb-2015"
    }]
},
{
    Title:"Dummy3",
    Events:[{
    eventName:"StartDate",
    Date:"Feb-2014"
    },
    {
    eventName:"EndDate",
    Date:"Mar-2015"
    }]
}];

var startDates = [],
    endDates = [];

for (i = 0; i < data.length; i++) {
  startDates.push(Date.parse(data[i].Events[0].Date));
  endDates.push(Date.parse(data[i].Events[1].Date));
  
}

Array.min = function( array ){
return Math.min.apply( Math, array );
};

Array.max = function( array ){
return Math.max.apply( Math, array );
};

// or use a library like Moment.js to format dates
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];


var minDate = new Date(Array.min(startDates)),
    minDateFormat = months[minDate.getMonth()]+'-'+minDate.getFullYear();

var maxDate = new Date(Array.max(endDates)),
    maxDateFormat = months[maxDate.getMonth()]+'-'+maxDate.getFullYear();



console.log(minDateFormat );
console.log(maxDateFormat);

๐ŸŒ
CodePen
codepen.io โ€บ nasyxnadeem โ€บ pen โ€บ VwyYGZm
get max date(most recent date) in an array of JavaScript date objects
By selecting a package, an import statement will be added to the top of the JavaScript editor for this package. ... Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ date โ€บ min or max date
Find the minimum or maximum date using JavaScript - 30 seconds of code
January 6, 2024 - const minDate = (...dates) => new Date(Math.min(...dates)); const maxDate = (...dates) => new Date(Math.max(...dates)); const dates = [ new Date('2017-05-13'), new Date('2018-03-12'), new Date('2016-01-10'), new Date('2016-01-09') ]; ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ sort-array-based-on-min-and-max-date-in-javascript
Sort array based on min and max date in JavaScript?
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));
Top answer
1 of 2
7

Use Array#sort with custom sort function and get the last(max) and first(min) values.

data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];

var sorted = data.slice() // copy the array for keeping original array with order
  // sort by parsing them to date
  .sort(function(a, b) {
    return new Date(a) - new Date(b);
  });

// get the first and last values
console.log(
  'max :', sorted.pop(), 'min :', sorted.shift()
);


Or with a simple Array#forEach loop.

data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];

// initially set max and min as first element
var max = data[0],
  min = data[0];

// iterate over array values and update min & max
data.forEach(function(v) {
  max = new Date(v) > new Date(max)? v: max;
  min = new Date(v) < new Date(min)? v: min;
});

console.log('max :', max, 'min :', min);

2 of 2
0

IMO, you should use new Date(value)

Nothing seems wrong in your code.

The reason of getting Invalid date is you're not converting date into Date object, you need to convert it in JavaScript date by new Date().

The Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)

var datesNew=["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
//This will convert your date into Date object
$.each(datesNew, function(key, value){
    dates.push(new Date(value));
});
var min = dates.reduce(function (a, b) { return a < b ? a : b; }); 
var max = dates.reduce(function (a, b) { return a > b ? a : b; }); 

var datesNew=["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
var dates=[];
$.each(datesNew, function(key, value){
    //Conver date in Date object
    dates.push(new Date(value));
});

var min = new Date(Math.min.apply( null, dates));
var max =  new Date(Math.max.apply( null, dates));
//var min = dates.reduce(function (a, b) { return a < b ? a : b; }); 
//var max = dates.reduce(function (a, b) { return a > b ? a : b; }); 
console.log(new Date(min).toString());
console.log(new Date(max).toString());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

๐ŸŒ
ServiceNow Community
servicenow.com โ€บ community โ€บ developer-forum โ€บ how-to-get-a-latest-date-from-an-array โ€บ m-p โ€บ 1979830
Solved: how to get a latest date from an array - ServiceNow Community
June 1, 2024 - function max_date(all_dates) { var max_dt = all_dates[0], max_dtObj = new GlideDateTime(all_dates[0]); all_dates.forEach(function(dt, index) { if ( new GlideDateTime( dt ) > max_dtObj) { max_dt = dt; max_dtObj = new GlideDateTime(dt); } }); return max_dt; } gs.print(max_date(["2019-08-28",...
๐ŸŒ
Piyush Mishra
codingwithpiyush.com โ€บ home โ€บ blog โ€บ getting the max/min dates in an array of objects with javascript
Getting The Max/Min Dates In An Array Of Objects With JavaScript | Piyush Mishra
December 22, 2023 - let dates = [ { date: new Date('2022-01-01') }, { date: new Date('2022-02-15') }, { date: new Date('2022-03-10') } ]; let maxDate = new Date(Math.max(...dates.map(obj => obj.date.getTime()))); let minDate = new Date(Math.min(...dates.map(obj ...
๐ŸŒ
Decipher
decipher.dev โ€บ maxdate
maxDate | 30 Seconds of Typescript - Decipher.dev
Use the ES6 spread syntax with Math.max to find the maximum date value, new Date() to convert it to a Date object. ... const array = [ new Date(2017, 4, 13), new Date(2018, 2, 12), new Date(2016, 0, 10), new Date(2016, 0, 9), ]; maxDate(array); ...