DateTime.ParseExact赋予String未被识别为有效的DateTime.

Des*_*yDK 18 c# asp.net datetime parsing

我正在尝试将日期字符串解析为DateTime变量.我发现这ParseExact是做到这一点的方法,但我试试这个我得到错误:

字符串未被识别为有效的DateTime.

string timeFormat = "dd-MM-yyyy hh:mm:ss";
DateTime startDate = DateTime.ParseExact(reader["startdate"].ToString(), timeFormat, CultureInfo.InvariantCulture);
DateTime nextDate = DateTime.ParseExact(reader["nextdate"].ToString(), timeFormat, null);
Run Code Online (Sandbox Code Playgroud)

我已经尝试了两种null(它恰好在另一页上工作)和CultureInfo.InvariantCulture.

reader["startdate"].ToString() 输出:01-08-2012 15:39:09

reader["nextdate"].ToString() 输出:01-08-2012 15:39:09

我认为它应该有效,但事实并非如此.

有人知道出了什么问题?:)

Jon*_*eet 46

您正在使用hh格式字符串.这是一个12小时的"一天一小时"字段.值15不在范围内......

你想要的HH是24小时说明符.

有关详细信息,请参阅MSDN自定义日期和时间格式字符串文档.