0

Im try to get the min and max date from an array in Javascript but I'm getting this error NaN

here the little code:

var dates = [];


var fecha1 =  "2020-05-05 15:00:00";
var fecha2 =  "2020-05-05 16:00:00";
var fecha3 =  "2020-05-06 13:00:00";
                dates.push(fecha1);
                dates.push(fecha2);
 dates.push(fecha3);

console.log(dates);

  var maximumDate=Math.max.apply(null, dates);
  var minimumDate=new Date(Math.min.apply(null, dates));
console.log(maximumDate);

I hope i explained well

MoteCL
  • 228
  • 3
  • 20

1 Answers1

1

Solution is of coarse here on SO: https://stackoverflow.com/a/7143443/7158959 You are already using first suggestion from that link, you should just try second one. Flag in it as duplicate.

var dates = [];


var fecha1 =  "2020-05-05 15:00:00";
var fecha2 =  "2020-05-05 16:00:00";
var fecha3 =  "2020-05-06 13:00:00";
                dates.push(fecha1);
                dates.push(fecha2);
 dates.push(fecha3);

console.log(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(min);
console.log(max);
ikiK
  • 6,328
  • 4
  • 20
  • 40