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

javascript - Find minimum and maximum dates - Stack Overflow

programmeradmin3浏览0评论

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 plex as I'm not very experienced in this language.

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 plex as I'm not very experienced in this language.

Share Improve this question edited Sep 12, 2012 at 14:55 piokuc 26.2k11 gold badges75 silver badges104 bronze badges asked Sep 12, 2012 at 14:51 You KuperYou Kuper 1,1137 gold badges19 silver badges38 bronze badges 13
  • Does your array contain date strings, or Date objects? – gen_Eric Commented Sep 12, 2012 at 14:56
  • possible duplicate of Find the oldest date in a list of dates – Bergi Commented Sep 12, 2012 at 14:56
  • @Rocket: Strings, it would have worked with Date objects ;-) – Bergi Commented Sep 12, 2012 at 14:57
  • 2 Just to add something - convert your date to unix timestamp and you can use Math.min.apply and Math.max.apply because unix timestamp is a single integer. – David Bélanger Commented Sep 12, 2012 at 15:13
  • 1 @Rocket Converting them to objects is a way to convert them to timestamps. You can easily get number of milliseconds since 1970-01-01 00:00:00 UTC (without leap seconds). – some Commented Sep 12, 2012 at 15:36
 |  Show 8 more ments

5 Answers 5

Reset to default 5

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

Math.min/max only pares numbers, not strings. Don't use them to represent the dates, but use Date objects - they will be pared 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];

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

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.

Is the array filled with Date objects? If so, pare 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.

发布评论

评论列表(0)

  1. 暂无评论