ramsey_lewis
ramsey_lewis

Reputation: 598

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 ?

Upvotes: 3

Views: 7009

Answers (2)

Pranav C Balan
Pranav C Balan

Reputation: 115222

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);

Upvotes: 7

Mohit Tanwani
Mohit Tanwani

Reputation: 6628

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>

Upvotes: 0

Related Questions