.net 字符串在 ParseExact 中未被识别为有效的日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16935816/
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 12:17:34  来源:igfitidea点击:

String was not recognized as a valid DateTime in ParseExact

.netvb.netdatetime

提问by iamCR

When I try to convert the DateTimeto a specific format i'm getting this error.

当我尝试将 转换DateTime为特定格式时,出现此错误。

DateTime.Now= 6/5/2013 2:29:21 PM

DateTime.ParseExact(CStr(DateTime.Now), "MM/dd/yyyy",  CultureInfo.CurrentCulture)

Error:

错误:

String was not recognized as a valid DateTime 

Why i'm getting this?

为什么我得到这个?

回答by Sean Airey

Apart from the fact that you're converting a DateTime to a string then back again, the DateTime format doesn't match exactly.

除了您将 DateTime 转换为字符串然后再转换回来之外,DateTime 格式并不完全匹配。

DateTime.ParseExactparses a string into a DateTime object, and the format you provide it must match exactly. You said that DateTime.Nowappears as 6/5/2013 2:29:21 PM, for which the correct format is M/d/yyyy h:mm:ss tt. Check MSDNfor more information on custom date formats.

DateTime.ParseExact解析字符串转换成DateTime对象,你提供它必须匹配的格式完全相同。您说DateTime.Now显示为6/5/2013 2:29:21 PM,其正确格式是M/d/yyyy h:mm:ss tt。有关自定义日期格式的更多信息,请查看MSDN

I'm going to go out on a limb and say that, by looking at your code, I think you are trying to format the date into just the date, which can be achieved using the ToStringmethod on DateTime:

我打算大胆地说,通过查看您的代码,我认为您正在尝试将日期格式化为日期,这可以使用ToStringDateTime 上的方法来实现:

string todaysDate = DateTime.Now.ToString("MM/dd/yyyy"); // todaysDate will contain "06/05/2013"

回答by Buh Buh

6/5/2013 2:29:21 PMis not the same as MM/dd/yyyy.
So of course the parse fails.

6/5/2013 2:29:21 PM不一样MM/dd/yyyy
所以当然解析失败。

From your comments it sounds like you are really testing the format string, and you don't care about the value of the date. So why not just hard code your date in the format you really want:

从您的评论看来,您确实是在测试格式字符串,而您并不关心日期的值。那么为什么不以您真正想要的格式对您的日期进行硬编码:

String userInput = "MM/dd/yyyy";
DateTime.ParseExact("11/11/2011", userInput,  CultureInfo.CurrentCulture)

回答by Lasse V. Karlsen

Note the part of the method named Exact, you're giving it a string containing a time, and does not specify how to parse the time, so the parsing will fail.

请注意名为Exact的方法部分,您为其提供了一个包含时间的字符串,并且没有指定如何解析时间,因此解析将失败。

Try this:

尝试这个:

DateTime.ParseExact(str, "M/d/yyyy h:mm:ss tt", CultureInfo.CurrentCulture)

Example LINQPadprogram:

示例LINQPad程序:

void Main()
{
    Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");

    string str = "6/5/2013 2:29:21 PM";

    DateTime.ParseExact(str, "M/d/yyyy h:mm:ss tt", CultureInfo.CurrentCulture).Dump();
}

Output:

输出:

6/5/2013 2:29:21 PM