C# DateTime.ParseExact FormatException String 未被识别为有效的 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15900226/
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
DateTime.ParseExact FormatException String was not recognized as a valid DateTime
提问by Colin Mackay
I'm completely stumped on this one. As far as I can see the documentation and other posts on SO I've read say this should work. I must be missing something silly, but I just cannot see it.
我完全被这个难住了。据我所知,SO 上的文档和其他帖子我读过说这应该有效。我一定错过了一些愚蠢的东西,但我就是看不到它。
I get a FormatException with the message "String was not recognized as a valid DateTime." on the following line of code:
我收到带有消息“字符串未被识别为有效日期时间”的 FormatException。在以下代码行中:
return DateTime.ParseExact(value, DateFormat, null,
DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal);
value
is"11/04/2013"
DateFormat
is"dd/MM/yyyy"
- The current culture is
en-GB
- I've tried various variants of
DateTimeStyles
but to no effect.
value
是"11/04/2013"
DateFormat
是"dd/MM/yyyy"
- 现在的文化是
en-GB
- 我尝试了各种变体,
DateTimeStyles
但没有效果。
My original intent was for the format ddd, dd/MMM/yyyy
but that didn't work either (the value in that instance was Tue, 30/Apr/2013
)
我的初衷是针对格式,ddd, dd/MMM/yyyy
但这也不起作用(该实例中的值为Tue, 30/Apr/2013
)
I've also tried forcing the culture to en-GB by passing in new CultureInfo("en-GB")
instead of the null
我还尝试通过传入new CultureInfo("en-GB")
而不是通过null
I also extracted the code into its own console application to see if there was different about the environment (ASP.NET MVC 3)
我还将代码提取到自己的控制台应用程序中,以查看环境是否有所不同(ASP.NET MVC 3)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var DateFormat = "dd/MM/yyyy";
var value = "30/04/2013";
var culture = new CultureInfo("en-GB");
var result = DateTime.ParseExact(value, DateFormat, culture,
DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal);
Console.WriteLine("\"{0}\" as \"{1}\" ==> {2}", value, DateFormat, result);
Console.ReadKey();
}
}
}
And that still gives me the same error.
这仍然给我同样的错误。
采纳答案by Rohit
Does this work
这行得通吗
string myDate = "30-12-1899 07:50:00:AM";
DateTime dt1 = DateTime.ParseExact(myDate, "dd-MM-yyyy hh:mm:ss:tt",
CultureInfo.InvariantCulture)
回答by Diorrini11
string myDate = "30-12-1899 07:50:00:AM";
DateTime dt1 = DateTime.ParseExact(myDate, "dd-MM-yyyy HH:mm:ss:tt",
CultureInfo.InvariantCulture);
Note the use of HH (24-hour clock) rather than hh (12-hour clock), and the use of InvariantCulture because some cultures use separators other than slash.
请注意使用 HH(24 小时制)而不是 hh(12 小时制),以及使用 InvariantCulture,因为某些文化使用除斜线以外的分隔符。
For example, if the culture is de-DE, the format "dd/MM/yyyy" would expect period as a separator (31.01.2011).
例如,如果区域性是 de-DE,则格式“dd/MM/yyyy”将期望句点作为分隔符 (31.01.2011)。