6

I have sample data as:

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"]
]

How can I get the max date value and min date value from the above data? The data may be not in sorted order.

JJJ
  • 32,902
  • 20
  • 89
  • 102
mpsbhat
  • 2,733
  • 12
  • 49
  • 105
  • What tool/platform/language are you using? (Please add to the tags.) – Richard Jun 08 '15 at 12:02
  • Check if it helps... [Fiddle](http://jsfiddle.net/c560t13n/). Also have a look @ [Min/Max of dates in an array](http://stackoverflow.com/a/7143601/769678) – Shubh Jun 08 '15 at 12:11

7 Answers7

19

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));
folkol
  • 4,752
  • 22
  • 25
  • Is there an implicit conversion from Date to int in javascript or is the `max`\`min` function doing a conversion? – xr280xr Jul 13 '22 at 17:26
  • In this case, `Math.max` is doing the conversion. See https://262.ecma-international.org/13.0/#sec-math.max for details. – folkol Jul 13 '22 at 19:27
2
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]);
shawn
  • 4,305
  • 1
  • 17
  • 25
1

Try this it will help you:

$(document).ready(function(){

var data = [
 [10, 622, 782, 783, "2015-05-05"],
  [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"],

];
    var dates = [];
    var max_date='';
    var min_date='';
    $.each(data, function(k,v){
          dates.push(v.pop());
          dates.sort(function(a,b){
              return new Date(a)- new Date(b);
            });
        max_date = dates[dates.length-1];
        min_date = dates[0];
    });
    console.log('max_date : '+max_date);
    console.log('min_date : '+min_date);
})
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
0

Working example Here

var a = [
  [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 max = a[0][4];
var min = a[0][4];
for (var i = 0; i < a.length; i++) {
  if (a[i][4] > max) {
    max = a[i][4];
  } else if (a[i][4] < min) {
    min = a[i][4];
  }
}
console.log(max);
console.log(min);

Reference from here

vamsikrishnamannem
  • 4,817
  • 5
  • 23
  • 34
0

Use Math.min() and Math.max() function along with Array.prototype property.

JS: View jsFiddle

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"]
];

dates = [];                           // Array store only dates
for (i = 0; i < data.length; i++) { 
    dates.push(new Date(data[i][4]));
}
console.log(dates);

Array.prototype.max = function() {    // find max dates
  return Math.max.apply(null, this);
};

Array.prototype.min = function() {    // find min dates
  return Math.min.apply(null, this);
};


alert("Max: "+new Date(dates.max())+"\n\n"+
      "Min: "+ new Date(dates.min()));
Community
  • 1
  • 1
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
0
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 dates = [];
for (var i=0;i<data.length;i++) { 
    dates.push(data[i][4]);
}

//sort the date

dates.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(b.date) - new Date(a.date);
});

// get min and max

var min_date = dates[0];
var max_date = dates.slice(-1)[0] 
nehal gala
  • 131
  • 1
  • 8
0

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 di = 4,
  maxd = mind = data[0][di];
var min = max = new Date(maxd).getTime(),
  dt, i = data.length;
while (i--) {
  dt = new Date(data[i][di]).getTime();
  if (dt > max) {
    max = dt;
    maxd = data[i][di];
  }
  if (dt < min) {
    min = dt;
    mind = data[i][di];
  }
}
console.log('min', mind, 'max', maxd);