0

我正在尝试以字符串格式转换包含日期的字符串数组

 private string[] ex = new string[]{
         "29-06-2017","29-12-2016","30-06-2016","31-12-2015","25-06-2015","24-12-2014","26-06-2014","26-12-2013"};

我想转换为十进制数组,我使用了下面的代码,但它不起作用。

 public void load()
    {
        DateTime[] exDate=Array.ConvertAll(ex, new Converter<string, DateTime>(convertDecimal)); 
        List<DateTime> expiryDate = new List<DateTime>();
        expiryDate.AddRange(exDate);
        expiryDate.Sort();
        _expiryDate=expiryDate;
    }

    public static DateTime convertDecimal(string strgDate)
    {
        return DateTime.Parse(strgDate);
    }

我得到的错误是:

"String was not recognized as a valid DateTime."
4

2 回答 2

1
var dates = ex.Select(d => DateTime.ParseExact(d, "dd-MM-yyyy", CultureInfo.InvariantCulture))
              .ToList();
于 2012-09-01T07:38:25.797 回答
0

尝试像这样转换:

public static DateTime convertDecimal(string strgDate)
{
    return DateTime.ParseExact(strgDate, "yyyy-MM-dd",
                    System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None));

}
于 2012-09-01T07:36:06.607 回答