代码:
var str = "91212";
DateTime.ParseExact(str, "Hmmss", System.Globalization.CultureInfo.CurrentCulture);
错误:
"91212" is not a valid DateTime
似乎c#尝试使用H
来匹配91
,这是不正确的时间。
如何解决?
答案 0 :(得分:3)
答案 1 :(得分:1)
我认为您必须强制它为6个字符并相应地定义格式字符串。以下代码:
var str = "91212";
if (str.Length == 5)
{
str = "0" + str;
}
var dtDate = DateTime.ParseExact(str, "HHmmss", System.Globalization.CultureInfo.CurrentCulture);
System.Diagnostics.Debug.WriteLine(dtDate.ToShortTimeString());
结果:
9:12 AM