javascript 从数组中获取最大和最小日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30708625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:34:21  来源:igfitidea点击:

Get max and min dates from an array

javascriptjqueryarraysdate

提问by mpsbhat

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.

如何从上述数据中获取最大日期值和最小日期值?数据可能没有排序。

回答by folkol

1)Use mapto extract the dates:

1)使用map提取dates

var dates = data.map(function(x) { return new Date(x[4]); })

2)Use Math.max/ Math.minto get the highest / lowest dates:

2)使用Math.max/Math.min获取最高/最低日期:

var latest = new Date(Math.max.apply(null,dates));
var earliest = new Date(Math.min.apply(null,dates));

回答by shawn

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

回答by vamsikrishnamannem

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

这里参考

回答by Jay Patel

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

使用Math.min()Math.max()函数以及Array.prototype属性。

JS: View jsFiddle

JS:查看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()));

回答by nehal gala

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] 

回答by PHP Worm...

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);
})

回答by PHP Worm...

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);