DateTime.ParseExact给出String未被识别为有效的DateTime. [英] DateTime.ParseExact gives String was not recognized as a valid DateTime.
问题描述
我正在尝试将日期字符串解析为DateTime
变量.我发现ParseExact
是执行此操作的方法,但是尝试此操作时出现错误:
I'm trying to parse a date string into a DateTime
variable. I've found out that ParseExact
is the way to do it, but I try this I get the error:
字符串未被识别为有效的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);
我已经尝试过使用null
(恰好在另一页上工作)和CultureInfo.InvariantCulture
.
I've tried both with null
(which happens to work on another page), and the CultureInfo.InvariantCulture
.
reader["startdate"].ToString()
输出:01-08-2012 15:39:09
reader["startdate"].ToString()
output: 01-08-2012 15:39:09
和
reader["nextdate"].ToString()
输出:01-08-2012 15:39:09
reader["nextdate"].ToString()
output: 01-08-2012 15:39:09
我认为它应该工作,但是不行.
I think it should work, but it doesn't.
有人知道什么地方错了吗? :)
Somebody have an idea what is wrong? :)
推荐答案
您在格式字符串中使用hh
.这是一个12个小时的一天中的一天"字段.值15不在范围内...
You're using hh
in your format string. That's a 12-hour "hour of day" field. The value 15 isn't in range...
您要使用HH
,它是24小时说明符.
You want HH
instead, which is the 24-hour specifier.
有关更多信息,请参见 MSDN自定义日期和时间格式字符串文档信息.
See the MSDN custom date and time format strings documentation for more information.
这篇关于DateTime.ParseExact给出String未被识别为有效的DateTime.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!