我正在尝试将以下字符串解析为DateTime:
“ 04-03-2008-16-18-08”
为简化起见,我编写了此单元测试,该单元测试始终会引发异常“ System.FormatException:字符串未被识别为有效的DateTime。”
[TestMethod]
public void TemporaryDateTimeParseTest()
{
DateTime.ParseExact("04-03-2008-16-18-08", "dd-MM-yyyy-hh-mm-ss", null);
}
格式应为日-月-年-时-分-秒(从上面可以明显看出)。根据我对MSDN的描述,这似乎是正确的。我想念什么?
我见过有人说ParseExact很尴尬,如果使用的方法不正确,我该如何使用常规的Parse方法来选择这种格式?
最佳答案
您需要将HH
用于24小时制而不是hh
用于12小时制。
MSDN Custom Date and Time Format Strings
关于c# - 使用ParseExact解析DateTime,我缺少什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12843872/