如何在数组中找到最大日期?

时间:2016-11-16 10:45:55

标签: jquery

如何从阵列中获取最大日期?我试过这个,但它返回无效日期

var dates = [2016/11/10,2016/11/20,2016/11/30]
var maxDate = new Date(Math.max.apply(null, dates));

任何人都可以分享任何想法以找到最长日期。

我用Jsfiddle在这里锻炼就是代码

var data = [ ['2015/04/21'], ['2015/04/21'], ['2015/04/22'], ['2015/05/23'], ['2015/04/24'], ['2015/04/29']]

var dates = []; for (i = 0; i < data.length; i++) { dates.push(new Date(data[i][0])); }

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

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

alert("Max: "+new Date(dates.max())+"\n\n"+ "Min: "+ new Date(dates.min()));

哪个在jsfiddle上工作正常但在开发环境上没有工作

2 个答案:

答案 0 :(得分:2)

&#13;
&#13;
  function find(){ 
					
		var data = [['2015/04/21'], ['2015/04/21'], ['2015/04/22'], ['2015/05/23'], ['2015/04/24'], ['2015/04/29']]
		var dates = [];
		for (i = 0; i < data.length; i++) 
		{
			dates.push(new Date(data[i][0]));
		}
			
		var maxDate=new Date(Math.max.apply(null,dates));
		alert(maxDate);
		
	}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<a href="#" class="copy-times"  onclick="find()">find max</a>
&#13;
&#13;
&#13;

答案 1 :(得分:-3)

var fmdt = fncgetDBFormatDate($("#<%=txt_Date.ClientID %>").val());

var todt = max();

if (fmdt != '' && todt != '') {
  if (fmdt > todt) {
    errMsg = "Date should be lesser than cheque maximum date ";
  }
}

function max() {
  var dates = [];
  var data1 = $("#jqxgrid2").jqxGrid('getrows');
  for (i = 0; i < data1.length; i++) {
    var val1 = $('#jqxgrid2').jqxGrid('getcellvalue', i, "Date");
    dates.push(new Date(convert_date(val1)));
  }

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

  var maxdate = new Date(Math.max.apply(null, dates));
  return convert_date(maxdate);
}

function convert_date(str) {
  var date = new Date(str),
    mnth = ("0" + (date.getMonth() + 1)).slice(-2),
    day = ("0" + date.getDate()).slice(-2);
  return [date.getFullYear(), mnth, day].join("/");
}