10

我有一个包含 Dates 的列表:

List<string> StringDates;

    [0]: "04.03.2010"
    [1]: "09.03.2010"
    [2]: "11.03.2010"
    [3]: "12.03.2010"
    [4]: "16.03.2010"
    [5]: "18.03.2010"
    [6]: "19.03.2010"
    [7]: "23.03.2010"
    [8]: "25.03.2010"
    [9]: "26.03.2010"

使用 C# 从这个列表中找出最小日期/最大日期的最佳/最短方法是什么?

4

6 回答 6

28

将它们转换为DateTime使用 ParseExact(或 TryParseExact),然后使用 Linq 获取最小值和最大值:

List<DateTime> dates = StringDates
   .Select(x => DateTime.ParseExact(x, "dd.MM.yyyy", null))
   .ToList();
DateTime minDate = dates.Min();
DateTime maxDate = dates.Max();

请注意,在您的示例中,列表已经按升序排序。如果您可以保证始终如此并且想要更好的性能,则可以只取第一个和最后一个元素,即 O(1) 而不是 O(n)。但是上面的内容更安全,因此即使您列出的列表可能总是被排序,除非您确实需要它,否则您可能不应该进行此优化,以防有一天列表没有按排序顺序排列。

于 2010-02-25T21:38:52.143 回答
21

使用 linq!:

    var list = new List<DateTime>();
    list.Add(new DateTime(2010, 1, 1));
    list.Add(new DateTime(2008, 1, 1));
    list.Add(new DateTime(2009, 1, 1));
    Console.WriteLine(list.Max(date => date));
    Console.WriteLine(list.Min(date => date));
于 2010-02-25T21:36:08.167 回答
19

我喜欢简单的解决方案。

DateTime minDate = DateTime.MaxValue;
DateTime maxDate = DateTime.MinValue;
foreach (string dateString in StringDates)
{
    DateTime date = DateTime.Parse(dateString);
    if (date < minDate)
        minDate = date;
    if (date > maxDate)
        maxDate = date;
}
于 2010-02-25T21:39:45.470 回答
2

通过 Linq,您可以执行以下操作:

from row in StringDates
group row by true into r 
select new { 
    min = r.Min(z => z), 
    max = r.Max(z => z) 
}
于 2010-02-25T21:33:48.027 回答
0

这类似于@Jeffrey 的答案,但不是解析每个日期,而是首先找到比较其字符串值的最小和最大日期,然后解析最后的值。

// This method handles the date comparisons
private int WeirdComparer(string strDate1, string strDate2)
{
    int res = string.Compare(strDate1, 6, strDate2, 6, 4);
    if (res == 0)
        res = string.Compare(strDate1, 3, strDate2, 3, 2);
    if (res == 0)
        res = string.Compare(strDate1, 0, strDate2, 0, 2);
    return res;
}

public void FindMinAndMaxDates(IList<string> strDates, out DateTime minDate, out DateTime maxDate)
{
    string min = "99.99.9999";
    string max = "00.00.0000";
    foreach (string strDate in strDates)
    {
        if (WeirdComparer(strDate, min) < 0)
            min = strDate;
        if (WeirdComparer(strDate, max) > 0)
            max = strDate;
    }
    minDate = DateTime.ParseExact(min, "dd.MM.yyyy", null);
    maxDate = DateTime.ParseExact(max, "dd.MM.yyyy", null);
}
于 2010-02-25T22:27:58.527 回答
0

我知道这不是您问题的直接答案,但如果他们来这里寻找类似的东西,可以帮助其他人。

我今天在尝试从对象列表中查找最大日期时遇到了这个问题。有时对象的日期中不会有值,所以我必须弄清楚如何在我的 linq 中使用 try 解析。

从马克在这里使用的组合中,我想出了这个来解决我的问题

        List<string> stringDates = new List<string>();
        stringDates.Add("Not a date");
        stringDates.Add("2015-01-01");
        stringDates.Add("2015-10-10");
        stringDates.Add("2015-9-10");
        stringDates.Add("123");

        DateTime sapInvoiceDate;
        DateTime tempDate = DateTime.MinValue;
        sapInvoiceDate = stringDates.Select(x => DateTime.TryParse(x, out tempDate)).Select(x => tempDate).Max();  
于 2015-08-27T15:29:23.540 回答