简体   繁体   中英

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:

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 ?

Use 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.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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