简体   繁体   English

如何使用javascript / jquery从数组中获取最大和最小日期?

[英]How to get max and min dates from array using javascript/jquery?

i want to get the min and max date form a json array: 我想从json数组中获取最小和最大日期:

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

but i have an error : Invalid date . 但我有一个错误: Invalid date can you help me ? 你能帮助我吗 ?

Use Array#sort with custom sort function and get the last(max) and first(min) values. 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() ); 


Or with a simple Array#forEach loop. 或使用简单的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, you should use new Date(value) IMO,您应该使用新的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() . 获取Invalid date的原因是您没有将date转换为Date对象,而是需要通过new Date()在JavaScript date中将其转换。

The Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds) 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM