[英]How to get max and min dates from array using javascript/jquery?
我想从json数组中获取最小和最大日期:
我的代码
$.getJSON(url, function (data) {
var dates = Object.keys( data['data']['horodate'] ).map(function ( key ) {
return data['data']['horodate'][key].replace(/\-/g,'/' )
});
var min = new Date(Math.min.apply( null, dates ));
var max = new Date(Math.max.apply( null, dates));
});
data
数组是:
Array [
"2016/10/13 00:00:00",
"2016/10/13 00:30:00",
"2016/10/13 01:00:00",
"2016/10/13 01:30:00",
"2016/10/13 02:00:00",
"2016/10/13 02:30:00",
"2016/10/13 03:00:00",
"2016/10/13 03:30:00",
"2016/10/13 04:00:00",
"2016/10/13 04:30:00"
]
但我有一个错误: Invalid date
。 你能帮助我吗 ?
将Array#sort
与自定义排序功能一起使用,并获取last(max)和first(min)值。
data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"]; var sorted = data.slice() // copy the array for keeping original array with order // sort by parsing them to date .sort(function(a, b) { return new Date(a) - new Date(b); }); // get the first and last values console.log( 'max :', sorted.pop(), 'min :', sorted.shift() );
或使用简单的Array#forEach
循环。
data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"]; // initially set max and min as first element var max = data[0], min = data[0]; // iterate over array values and update min & max data.forEach(function(v) { max = new Date(v) > new Date(max)? v: max; min = new Date(v) < new Date(min)? v: min; }); console.log('max :', max, 'min :', min);
IMO,您应该使用新的Date(value)
您的代码中似乎没有什么错。
获取Invalid date
的原因是您没有将date转换为Date对象,而是需要通过new Date()
在JavaScript date中将其转换。
Date对象使您可以处理日期(年,月,日,小时,分钟,秒和毫秒)
var datesNew=["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
//This will convert your date into Date object
$.each(datesNew, function(key, value){
dates.push(new Date(value));
});
var min = dates.reduce(function (a, b) { return a < b ? a : b; });
var max = dates.reduce(function (a, b) { return a > b ? a : b; });
var datesNew=["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"]; var dates=[]; $.each(datesNew, function(key, value){ //Conver date in Date object dates.push(new Date(value)); }); var min = new Date(Math.min.apply( null, dates)); var max = new Date(Math.max.apply( null, dates)); //var min = dates.reduce(function (a, b) { return a < b ? a : b; }); //var max = dates.reduce(function (a, b) { return a > b ? a : b; }); console.log(new Date(min).toString()); console.log(new Date(max).toString());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.