gpt4 book ai didi

c# - DateTime.ParseExact 方法 : always throws exception even with correct format 的问题

转载 作者:太空狗 更新时间:2023-10-30 00:19:49 24 4
gpt4 key购买 nike

我在论坛中搜索了类似的解决方案,但没有找到真正符合我的特定问题的解决方案。

这可能需要更有经验的人才能找到问题所在,所以我感谢所有帮助!

问题:我正在尝试将带有日期的字符串解析为 DateTime 变量。但是,即使字符串日期格式完全相同,它仍然会抛出异常。

我想知道为什么,我该如何解决。我真的看不出哪里出了问题!

try
{
string value = "Sep-17-2012 03:04:07 am";

string format = "M-dd-yyyy hh:mm:ss tt";

DateTime temp = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
}
catch(Exception e){}

提前致谢

疯狂

最佳答案

您的格式应该是 MMM 而不是 M http://www.dotnetperls.com/datetime-format

string format = "MMM-dd-yyyy hh:mm:ss tt";

M - display one-digit month number

MMM - display a three letter month

关于c# - DateTime.ParseExact 方法 : always throws exception even with correct format 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16526220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com