DateTime.ParseExact FormatException
为什么以下代码会生成 FormatException?
DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", null);
也许这与以下事实有关:代码作为 IIS 7.5 Express 的一部分运行MVC3站点执行逻辑?
Why does the following code generate a FormatException?
DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", null);
Perhaps it has something to do with the fact that the code is running under IIS 7.5 Express as a part of an MVC3 site execution logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要包含 CultureInfo,例如:
格式字符串中的斜杠是区域性敏感的,如果您不传入 CultureInfo,则使用当前区域性。您还可以使用CultureInfo.InvariantCulture,它会起作用。 Jon Skeet 在此提供了一些详细的解释。
You need to include CultureInfo, for example:
Slashes in format string are culture sensitive and if you don't pass in CultureInfo, current culture is used. You can also use
CultureInfo.InvariantCulture
and it will work. Jon Skeet provides some detailed explanation here.取决于你的文化,将其排除在外......
depends on your culture, to take that out of the equation....
根据文档,
FormatException
是抛出,给定以下条件之一:如果您传入 null
IFormatProvider
,我认为它默认为当前线程的区域性。我必须在 Reflector 中看看这个。您有什么理由想传入null
吗?更新:
我在.NET Reflector中查看了它,它默认为当前线程的
DateTimeFormatInfo
。我不知道我是否可以在这里发布代码。According to the documentation, a
FormatException
is thrown, given one of these conditions:If you pass in a null
IFormatProvider
, I think it defaults to the current thread's culture. I'd have to look at this in Reflector. Is there any reason you wanted to pass innull
?UPDATE:
I looked at it in .NET Reflector and it defaults to the current thread's
DateTimeFormatInfo
. I don't know if I'm allowed to post the code here.