.net 为什么 DateTime.ParseExact() 不能使用“M/d/yyyy”解析“9/1/2009”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1368636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:16:13  来源:igfitidea点击:

Why can't DateTime.ParseExact() parse "9/1/2009" using "M/d/yyyy"

.netdatetimeparsingcultureinfo

提问by Jimmy

I have a string that looks like this: "9/1/2009". I want to convert it to a DateTime object (using C#).

我有一个看起来像这样的字符串:“9/1/2009”。我想将它转换为 DateTime 对象(使用 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.

日期中没有字词(例如“September”),而且我知道具体的格式,所以我宁愿使用 ParseExact(我不明白为什么需要 CultureInfo)。但是我不断收到可怕的“字符串未被识别为有效的日期时间”异常。

Thanks

谢谢

A little follow up. Here are 3 approaches that work:

一点跟进。以下是 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"));

And here are 3 that don't work:

这里有 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);

So, Parse() works with "en-US", but not ParseExact... Unexpected?

所以,Parse() 与“en-US”一起工作,但不能与 ParseExact 一起工作......出乎意料?

回答by Jon Skeet

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 nullmeans "use the current culture". If you eitherescape the slashes ("M'/'d'/'yyyy") oryou specify CultureInfo.InvariantCulture, it will be okay.

我怀疑问题在于格式字符串中的斜杠与数据中的斜杠。这是格式字符串中区分文化的日期分隔符,最后一个参数null表示“使用当前文化”。如果你要么逃避斜线(“M‘/’d‘/’YYYY”)您指定的CultureInfo.InvariantCulture,一切都会好的。

If anyone's interested in reproducing this:

如果有人有兴趣复制这个:

// Works
DateTime dt = DateTime.ParseExact("9/1/2009", "M'/'d'/'yyyy", 
                                  new CultureInfo("de-DE"));

// Works
DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy", 
                                  new CultureInfo("en-US"));

// Works
DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy", 
                                  CultureInfo.InvariantCulture);

// Fails
DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy", 
                                  new CultureInfo("de-DE"));

回答by Lee

I bet your machine's culture is not "en-US". From the documentation:

我敢打赌你的机器文化不是“en-US”。从文档

If provideris a null reference (Nothing in Visual Basic), the current culture is used.

如果provider是空引用(在 Visual Basic 中为 Nothing),则使用当前区域性。

If your current culture is not "en-US", this would explain why it works for me but doesn't work for you andworks when you explicitly specify the culture to be "en-US".

如果您当前的文化不是“en-US”,这将解释为什么它对我有用但对您不起作用,并且当您明确指定文化为“en-US”时。

回答by Scott

Try

尝试

Date.ParseExact("9/1/2009", "M/d/yyyy", new CultureInfo("en-US"))

回答by RRUZ

try this

尝试这个

provider = new CultureInfo("en-US");
DateTime.ParseExact("9/1/2009", "M/d/yyyy", provider);

Bye.

再见。

回答by BigOldSofty

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.

我在 XP 上尝试过,如果 PC 设置为国际时间 yyyy-Md,则它不起作用。在行上放置一个断点,在处理之前将日期字符串更改为使用“-”代替“/”,您会发现它有效。无论您是否拥有 CultureInfo 都没有区别。能够指定一个expercted 格式只忽略分隔符似乎很奇怪。

回答by RINESH

Try :

尝试 :

Configure in web config file

在 web 配置文件中配置

<system.web> <globalization culture="ja-JP" uiCulture="zh-HK" /> </system.web>

<system.web> <globalization culture="ja-JP" uiCulture="zh-HK" /> </system.web>

eg: DateTime dt = DateTime.ParseExact("08/21/2013", "MM/dd/yyyy", null);

例如:DateTime dt = DateTime.ParseExact("08/21/2013", "MM/dd/yyyy", null);

ref url : http://support.microsoft.com/kb/306162/

参考网址:http: //support.microsoft.com/kb/306162/

回答by Sandeep Ravi

Set DateTimePicker's Format property to custom and CustomFormatprperty to M/dd/yyyy.

将 DateTimePicker 的 Format 属性设置为 custom 并将CustomFormatprperty 设置为M/dd/yyyy.