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));
Answer from Andrew D. on Stack OverflowCode 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
Hello,
I would like to get the minimum date of this array of objects bellow:
let activities = [
{ title: 'Hiking', date: '2019-06-13' },
{ title: 'Shopping', date: '2019-06-10' },
{ title: 'Trekking', date: '2019-06-22' },
{ title: 'Trekking', date: null }
]
let sortedActivities = activities.sort((a, b) => new Date(a.date) - new Date(b.date))
console.log(sortedActivities[0])
The problem is when date activities.date is null it always returns null .
What's the best way to reject null value from this comparison ?
Thanks
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]);
If your array contains Date objects, then this should work. If it just contains strings like '2012-09-12 09:20', then you can sort them, and get the 1st and last elements.
a.sort(function(a, b){
return Date.parse(a) - Date.parse(b);
});
var maxT = a[a.length-1];
var minT = a[0];
Math.min/max only compares numbers, not strings. Don't use them to represent the dates, but use Date objects - they will be compared by their internal timestamp number. Still, the max/min will return that internal number, so you would need to convert it back to a Date (see Min/Max of dates in an array?):
However, if you want to use the strings or can't use the recreated Date, you will need to run manually through the array - either with a for-loop, or the ES5.1-only iterator method .reduce():
var min = datestrings.reduce(function(min, cur) {
return cur < min ? cur : min;
});
// is equivalent to
var min = datestrings[0];
for (var i=1; i<datestrings.length; i++)
if (datestrings[i] < min)
min = datestrings[i];
If your code does not need to be efficient, you also just can sort the array and get the first and last values. The default alphanumeric sorting will do it for your date format, so this is really simple:
datestrings.sort();
var min = datestrings[0],
max = datestrings[datestrings.lengh-1];