最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get max and min dates from array using javascriptjquery? - Stack Overflow

programmeradmin0浏览0评论

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 ?

Share Improve this question edited Oct 13, 2016 at 11:50 Mohammad 21.5k16 gold badges56 silver badges84 bronze badges asked Oct 13, 2016 at 10:30 ramsey_lewisramsey_lewis 5981 gold badge8 silver badges26 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

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

发布评论

评论列表(0)

  1. 暂无评论