javascript 查找最小和最大日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12390933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:04:31  来源:igfitidea点击:

Find minimum and maximum dates

javascriptdatedatetimemaxminimum

提问by You Kuper

There is an array:

有一个数组:

var a = new Array();

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

它包含这样的日期条目:“2012-09-12 09:20”等

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

我需要使用 javascript 查找最小和最大日期。此代码不适用于时间值。

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.

我怎样才能在javascript中解决这个问题?这似乎很复杂,因为我对这门语言不是很熟悉。

回答by Rocket Hazmat

If your array contains Dateobjects, 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.

如果您的数组包含Date对象,那么这应该可以工作。如果它只包含像 那样的字符串'2012-09-12 09:20',那么你可以对它们进行排序,并获得第一个和最后一个元素。

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

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

回答by Bergi

Math.min/maxonly compares numbers, not strings. Don't use them to represent the dates, but use Dateobjects- 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?):

Math.min/max只比较数字,而不是字符串。不要使用它们来表示日期,而是使用Date对象——它们将通过它们的内部时间戳编号进行比较。尽管如此,最大值/最小值仍将返回该内部数字,因此您需要将其转换回 a Date(请参阅数组中日期的最小值/最大值?):

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():

但是,如果您想使用字符串或不能使用重新创建的日期,则需要手动运行数组 - 使用 for 循环或仅 ES5.1 的迭代器方法.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];

回答by mornaner

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.

免责声明:仅当所有日期字符串的格式相同时,第二种方法才有效,如果数组中的日期格式不同,则将无法使用此函数。

回答by Luigi Siri

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

我在之前的一个问题中找到了它

回答by seth

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

数组是否填充了 Date 对象?如果是这样,请使用它们进行比较,并使用许多已知算法之一对它们进行排序。

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

如果没有,请使用 Date 对象重新创建数组,每个对象一个,并按照我上面所说的方法对数组进行排序。