问题描述:

This question already has an answer here:

  • Why I get wrong result from function?

    2 answers

网友答案:

The Math.max function assumes that everything in its arguments can be converted to a number. That isn't necessarily true of a Date (and apparently is screwing up, not surprisingly, in IE).

Does this work:

title.innerHTML = new Date(Math.max.apply(null, arr.map(function (e){  
         return new Date(e.MeasureDate).getTime();
   }))).toISOString();
网友答案:

One more solution. Doing all steps in a single line is not a good idea in terms of readability.

var maxDate = Math.max(...arr.map((e)=> new Date(e.MeasureDate)))
title.innerHTML = new Date(maxDate).toISOString();
网友答案:

If requirement is to return date string which is most recent, that is, greatest number, you can use String.prototype.match(), Array.prototype.sort()

var arr=[{Id:540, MeasureDate:"2016-06-27T15:06:47"},
             {Id:541, MeasureDate:"2016-06-27T12:24:39"}];

function n(d) {
  return d.match(/\d+/g).join("")
}

var res = arr.sort(function(a, b) {
  return +n(b.MeasureDate) - +n(a.MeasureDate)
})[0].MeasureDate;

document.body.textContent = res;
相关阅读:
Top