2009-09-17 19 views
6

C#2.0を使用してDateTime値のリストから最大datetimeを取得するにはどうすればよいですか?ここでリスト内の最大DateTime

+0

が必要です... more ... info ... ループ内でmax datetimeを取得していますか? 日時の最大値は?そのループは何をするのですか? –

+3

今月の**曖昧な質問に指名されました**。 – Welbog

+3

@Welbog:あなたの編集だけが問題を無意味にしたと思う。 –

答えて

3

これを行うには、単純なループです:

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

あなたがセット、コレクション、またはリストで最大日時を意味していますか?もしそうなら:あなたは任意の日時のために可能な限り最高のサポート値を知りたいわけ

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

場合は、それだけだ。

DateTime.MaxValue; 
17

すべての反復では何....これは非常に些細な

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

// This is the max... 
DateTime MaxDate = _Dates.Max(); 
+0

これは良いですが、.NET 3.5が必要です。 –

+0

新しい情報を追加してくれてありがとう...これは私が探していたものです –

関連する問題