toISOString() changes datetime value

后端 未结 2 1299
花落未央
花落未央 2021-01-04 11:31

I have array of the objects.Each object in array has date property.I try to get biggest(the last) date from array.

Here is array:

var sensorsData = [         


        
2条回答
  •  日久生厌
    2021-01-04 12:12

    The problem lay in that this process (that makes date conversion to integers than back to dates) is not reversible the proof is in the following function that performs conversion from dates to int and then back to dates, only to get different values from the starting ones

    
    

    My solution is pretty simple : comparing dates as are, and returning the result

    function setDate(){
    var sensorsData = [{Id:1,MeasureDate:"2017-08-20T09:52:32" },{Id:2,MeasureDate:"2017-08-20T09:54:35" },{Id:3,MeasureDate:"2017-08-20T09:56:13"}];
    
    var lastDate = updateLatestDate(sensorsData);
    console.log(lastDate.MeasureDate);
    
    
    function compare(d1,d2){
    if (d1.MeasureDate >d2.MeasureDate)
      return d1;
    else
      return d2;
    }
    
    function updateLatestDate(sensorsData) {
          return ( sensorsData.reduce(compare) );
      }
    }
    

提交回复
热议问题