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.
javascript - Min/Max of dates in an array? - Stack Overflow
javascript - Get max and min dates from an array - Stack Overflow
Finding the max date from an array of dates using javascript - Stack Overflow
javascript - How to get a string of max date from an array of string dates? - Stack Overflow
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.
Further to @Travis Heeter's answer, this returns the object that contains the latest date:
array.reduce((a, b) => (a.MeasureDate > b.MeasureDate ? a : b));
A more robust solution perhaps might be convert the strings into Date objects every time. Could be noticeably slower if dealing with (very) large arrays:
array.reduce((a, b) => {
return new Date(a.MeasureDate) > new Date(b.MeasureDate) ? a : b;
});
Code is tested with IE,FF,Chrome and works properly:
var dates=[];
dates.push(new Date("2011/06/25"))
dates.push(new Date("2011/06/26"))
dates.push(new Date("2011/06/27"))
dates.push(new Date("2011/06/28"))
var maxDate=new Date(Math.max.apply(null,dates));
var minDate=new Date(Math.min.apply(null,dates));
Something like:
var min = dates.reduce(function (a, b) { return a < b ? a : b; });
var max = dates.reduce(function (a, b) { return a > b ? a : b; });
Tested on Chrome 15.0.854.0 dev
1) Use map to extract the dates:
var dates = data.map(function(x) { return new Date(x[4]); })
2) Use Math.max / Math.min to get the highest / lowest dates:
var latest = new Date(Math.max.apply(null,dates));
var earliest = new Date(Math.min.apply(null,dates));
var data = [
[1, 622, 782, 783, "2015-04-21"],
[2, 622, 782, 783, "2015-04-21"],
[3, 622, 782, 783, "2015-04-22"],
[4, 622, 782, 783, "2015-04-23"],
[5, 622, 782, 783, "2015-04-24"],
[6, 622, 782, 783, "2015-04-28"],
[7, 622, 782, 783, "2015-04-28"],
[8, 622, 782, 783, "2015-04-29"],
[9, 622, 782, 783, "2015-05-04"],
[10, 622, 782, 783, "2015-05-05"]
];
var minIdx = 0, maxIdx = 0;
for(var i = 0; i < data.length; i++) {
if(data[i][4] > data[maxIdx][4]) maxIdx = i;
if(data[i][4] < data[minIdx][4]) minIdx = i;
}
alert('Max: ' + maxIdx + ', ' + data[maxIdx][4]);
alert('Min: ' + minIdx + ', ' + data[minIdx][4]);
Try wrapping document.getElementById('date1').value in new Date().getTime() .
Also input type="date" accepts value as yyyy-mm-dd ; try using .toJSON() , String.prototype.slice() to set date properly for #maxdate as yyyy-mm-dd from value returned as maxDate
function checkMaxDate() {
var dateArray = [];
var date1 = dateArray.push(new Date(document.getElementById('date1').value).getTime());
var date2 = dateArray.push(new Date(document.getElementById('date2').value).getTime());
var maxDate = Math.max.apply(Math, dateArray);
document.getElementById('maxdate').value = new Date(maxDate).toJSON().slice(0, 10);
}
Date 1:
<input type="date" id="date1" />Date 2:
<input type="date" id="date2" />Max Date:
<input type="date" id="maxdate" />
<button type="button" onclick="checkMaxDate()">Check</button>
You were close. Try applying the function inside a newDate, and null instead of Math. You should also new Date(pushedArrayElement) to be a Date object, and not push Strings into the date array.
var maxDate= new Date(Math.max.apply(null,dateArray));
Note that there's cause for error because it's not guaranteed that the user input will be a legal date format.
You could compare the ISO 8601 date like strings and take the greater value.
var dates = ["2018-12-29T15:23:20.486695Z", "2018-12-29T15:23:21.613216Z", "2018-12-29T15:23:22.695710Z", "2018-12-29T15:23:24.013567Z", "2018-12-29T15:23:25.097649Z", "2018-12-29T15:23:26.692125Z", "2018-12-29T15:23:27.918561Z", "2018-12-29T15:23:29.217879Z", "2018-12-29T15:23:30.468284Z", "2018-12-29T15:23:31.548761Z"],
latest = dates.reduce((a, b) => a > b ? a : b);
console.log(latest);
That's the format provided by toISOString on Date. So you take your timestamp value, feed it into new Date, and use toISOString on the result:
console.log(new Date(max_date).toISOString());
Example:
var dates = [
"2018-12-29T15:23:20.486695Z",
"2018-12-29T15:23:21.613216Z",
"2018-12-29T15:23:22.695710Z",
"2018-12-29T15:23:24.013567Z",
"2018-12-29T15:23:25.097649Z",
"2018-12-29T15:23:26.692125Z",
"2018-12-29T15:23:27.918561Z",
"2018-12-29T15:23:29.217879Z",
"2018-12-29T15:23:30.468284Z",
"2018-12-29T15:23:31.548761Z"
];
var timestamps = dates.map(date => Date.parse(date));
var max_date = Math.max.apply(Math, timestamps)
console.log(new Date(max_date).toISOString());
Note that you get "2018-12-29T15:23:31.548Z" rather than "2018-12-29T15:23:31.548761Z" because you've parsed the strings to JavaScript dates, and JavaScript dates only hold milliseconds, not microseconds.