我正在尝试转换包含字符串格式的日期的字符串数组
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."
答案 0 :(得分:1)
var dates = ex.Select(d => DateTime.ParseExact(d, "dd-MM-yyyy", CultureInfo.InvariantCulture))
.ToList();
答案 1 :(得分:0)
尝试像这样转换:
public static DateTime convertDecimal(string strgDate)
{
return DateTime.ParseExact(strgDate, "yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None));
}