質問

C#2.0を使用してDateTime値のリストから最大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();

set、collection、またはList の最大日時を意味しますか?その場合:

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