You Kuper
You Kuper

Reputation: 1113

Find minimum and maximum dates

There is an array:

var a = new Array();

It contains date entries like this: '2012-09-12 09:20', etc

I need to find minimum and maximum dates using javascript. This code does not work with time values.

var minT = Math.min.apply(Math, a);
var maxT = Math.max.apply(Math, a);

How can I solve this problem in javascript? It seems to be quite complex as I'm not very experienced in this language.

Upvotes: 2

Views: 16505

Answers (5)

Bergi
Bergi

Reputation: 664195

Math.min/max only compares numbers, not strings. Don't use them to represent the dates, but use Date objects - they will be compared by their internal timestamp number. Still, the max/min will return that internal number, so you would need to convert it back to a Date (see Min/Max of dates in an array?):

However, if you want to use the strings or can't use the recreated Date, you will need to run manually through the array - either with a for-loop, or the ES5.1-only iterator method .reduce():

var min = datestrings.reduce(function(min, cur) {
    return cur < min ? cur : min;
});

// is equivalent to
var min = datestrings[0];
for (var i=1; i<datestrings.length; i++)
    if (datestrings[i] < min)
        min = datestrings[i];

If your code does not need to be efficient, you also just can sort the array and get the first and last values. The default alphanumeric sorting will do it for your date format, so this is really simple:

datestrings.sort();
var min = datestrings[0],
    max = datestrings[datestrings.lengh-1];

Upvotes: 3

gen_Eric
gen_Eric

Reputation: 227190

If your array contains Date objects, then this should work. If it just contains strings like '2012-09-12 09:20', then you can sort them, and get the 1st and last elements.

a.sort(function(a, b){
    return Date.parse(a) - Date.parse(b);
});

var maxT = a[a.length-1];
var minT = a[0];

Upvotes: 5

mornaner
mornaner

Reputation: 2424

This should do it:

var maxT=new Date(Math.max.apply(null,a));
var minT=new Date(Math.min.apply(null,a));

If you must work with strings you could define a function:

function maxDate(data){
    var max = '';
    for(var i=0; i<data.length; i++)
        if(data[i]>max)
            max=data[i];
    return max;
}

And then:

var maxT=maxDate(a);

DISCLAIMER: This second method will only work if all the date strings are in the same format, if you have different format dates in your array you will not be able to use this function.

Upvotes: 1

seth
seth

Reputation: 1389

Is the array filled with Date objects? If so, compare them using them, and sort them using one of the many known algorithms.

If not, recreate the array with Date objects, one for each of them, and do as I said above, by ordering the array.

Upvotes: 0

Luigi Siri
Luigi Siri

Reputation: 2018

Try this:

var maxDate=new Date(Math.max.apply(null,dates));
var minDate=new Date(Math.min.apply(null,dates));

I found it on an earlier question

Upvotes: 1

Related Questions