为什么 DateTime.ParseExact() 无法解析“9/1/2009”使用“M/d/yyyy”
我有一个如下所示的字符串:“9/1/2009”。我想将其转换为 DateTime 对象(使用 C#)。
这有效:
DateTime.Parse("9/1/2009", new CultureInfo("en-US"));
但我不明白为什么这不起作用:
DateTime.ParseExact("9/1/2009", "M/d/yyyy", null);
日期中没有单词(例如“九月”),而且我知道具体格式,所以我宁愿使用 ParseExact (而且我没有看到为什么需要 CultureInfo)。但我不断收到可怕的“字符串未被识别为有效的日期时间”异常。
谢谢
一点后续。这里有 3 种有效的方法:
DateTime.ParseExact("9/1/2009", "M'/'d'/'yyyy", null);
DateTime.ParseExact("9/1/2009", "M/d/yyyy", CultureInfo.InvariantCulture);
DateTime.Parse("9/1/2009", new CultureInfo("en-US"));
这里有 3 种无效的方法:
DateTime.ParseExact("9/1/2009", "M/d/yyyy", CultureInfo.CurrentCulture);
DateTime.ParseExact("9/1/2009", "M/d/yyyy", new CultureInfo("en-US"));
DateTime.ParseExact("9/1/2009", "M/d/yyyy", null);
所以,Parse() 适用于“en-US”,但不适用于 ParseExact...意外吗?
I have a string that looks like this: "9/1/2009". I want to convert it to a DateTime object (using C#).
This works:
DateTime.Parse("9/1/2009", new CultureInfo("en-US"));
But I don't understand why this doesn't work:
DateTime.ParseExact("9/1/2009", "M/d/yyyy", null);
There's no word in the date (like "September"), and I know the specific format, so I'd rather use ParseExact (and I don't see why CultureInfo would be needed). But I keep getting the dreaded "String was not recognized as a valid DateTime" exception.
Thanks
A little follow up. Here are 3 approaches that work:
DateTime.ParseExact("9/1/2009", "M'/'d'/'yyyy", null);
DateTime.ParseExact("9/1/2009", "M/d/yyyy", CultureInfo.InvariantCulture);
DateTime.Parse("9/1/2009", new CultureInfo("en-US"));
And here are 3 that don't work:
DateTime.ParseExact("9/1/2009", "M/d/yyyy", CultureInfo.CurrentCulture);
DateTime.ParseExact("9/1/2009", "M/d/yyyy", new CultureInfo("en-US"));
DateTime.ParseExact("9/1/2009", "M/d/yyyy", null);
So, Parse() works with "en-US", but not ParseExact... Unexpected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我怀疑问题在于格式字符串中的斜杠与数据中的斜杠。这是格式字符串中区分区域性的日期分隔符,最后一个参数
null
表示“使用当前区域性”。如果您转义斜杠(“M'/'d'/'yyyy”)或您指定CultureInfo.InvariantCulture
,则它将是好的。如果有人有兴趣复制这个:
I suspect the problem is the slashes in the format string versus the ones in the data. That's a culture-sensitive date separator character in the format string, and the final argument being
null
means "use the current culture". If you either escape the slashes ("M'/'d'/'yyyy") or you specifyCultureInfo.InvariantCulture
, it will be okay.If anyone's interested in reproducing this:
我敢打赌你的机器的文化不是“en-US”。来自文档:
如果您当前的文化不是“en-US”,这可以解释为什么它对我有效但对您不起作用并且在您明确指定文化为“en-US”时有效。
I bet your machine's culture is not "en-US". From the documentation:
If your current culture is not "en-US", this would explain why it works for me but doesn't work for you and works when you explicitly specify the culture to be "en-US".
尝试
Try
试试这个
再见。
try this
Bye.
我在 XP 上尝试过,如果电脑设置为国际时间 yyyy-Md,则该方法不起作用。在该行上放置一个断点,然后在处理之前将日期字符串更改为使用“-”代替“/”,您会发现它有效。无论您是否拥有 CultureInfo,都没有什么区别。
能够指定专业格式却忽略分隔符似乎很奇怪。
I tried it on XP and it doesn't work if the PC is set to International time yyyy-M-d. Place a breakpoint on the line and before it is processed change the date string to use '-' in place of the '/' and you'll find it works. It makes no difference whether you have the CultureInfo or not.
Seems strange to be able specify an expercted format only to have the separator ignored.
将 DateTimePicker 的 Format 属性设置为 custom,并将
CustomFormat
属性设置为M/dd/yyyy
。Set DateTimePicker's Format property to custom and
CustomFormat
prperty toM/dd/yyyy
.尝试:
在网络配置文件中配置
<全球化文化="ja-JP" uiCulture="zh-HK" />
例如:DateTime dt = DateTime.ParseExact("08/21/2013", "MM/dd/yyyy", null);
参考网址:http://support.microsoft.com/kb/306162/
Try :
Configure in web config file
<system.web>
<globalization culture="ja-JP" uiCulture="zh-HK" />
</system.web>
eg: DateTime dt = DateTime.ParseExact("08/21/2013", "MM/dd/yyyy", null);
ref url : http://support.microsoft.com/kb/306162/