如何使用C#2.0从DateTime值列表中获取最大日期时间?

有帮助吗?

解决方案

这是一个简单的循环:

List<DateTime> dates = new List<DateTime> { DateTime.Now, DateTime.MinValue, DateTime.MaxValue };

DateTime max = DateTime.MinValue; // Start with the lowest value possible...
foreach(DateTime date in dates)
{
    if (DateTime.Compare(date, max) == 1)
        max = date;
}

// max is maximum time in list, or DateTime.MinValue if dates.Count == 0;

其他提示

所有迭代的内容......这非常简单

// Given...
List<DateTime> dates = { a list of some dates... }

// This is the max...
DateTime MaxDate = dates.Max();

您的意思是集合,集合或列表中的最大日期时间吗?如果是这样的话:

DateTime max = DateTime.MinValue;
foreach (DateTime item in DateTimeList)
{
    if (item > max) max = item;
}
return max;

如果您的意思是想知道任何日期时间支持的最高值,那就是:

DateTime.MaxValue;

var max = new [] {datetime1,datetime2} .Max();

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top