admin 管理员组文章数量: 1086019
i want to get the min and max date form a json array:
my code
$.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 is:
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"
]
but i have an error : Invalid date
. can you help me ?
i want to get the min and max date form a json array:
my code
$.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 is:
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"
]
but i have an error : Invalid date
. can you help me ?
2 Answers
Reset to default 7Use Array#sort
with custom sort function and get the last(max) and first(min) values.
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()
);
Or with a simple Array#forEach
loop.
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, you should use new Date(value)
Nothing seems wrong in your code.
The reason of getting Invalid date
is you're not converting date into Date object, you need to convert it in JavaScript date by new Date()
.
The Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)
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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
本文标签: How to get max and min dates from array using javascriptjqueryStack Overflow
版权声明:本文标题:How to get max and min dates from array using javascriptjquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744018872a2519434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论