[英]Getting error : String was not recognized as a valid DateTime in c#
Getting error like : 出现类似的错误:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: String was not recognized as a valid DateTime.
mscorlib.dll中发生了'System.FormatException'类型的未处理异常。其他信息:无法将字符串识别为有效的DateTime。
I am using this code : 我正在使用此代码:
string datetime = DateTime.Parse(encrypt[1]);
or 要么
string datetime = Convert.ToDatetime(encrypt[1]);
encrypt is a string array 加密是一个字符串数组
In encrypt[1]
I am not sure which format will come in string
. 在
encrypt[1]
我not sure which format will come in string
。 i have trace some time come dd/MM/yyyy
and sometime MM/dd/yyyy
or MM-dd-yyyy
or dd-MM-yyyy
. 我有一段时间来跟踪
dd/MM/yyyy
和MM/dd/yyyy
或MM-dd-yyyy
或dd-MM-yyyy
。 I am not sure about format it may from above or another format come. 我不确定它是从上面格式化的,还是其他格式来了。
also use ParseExcept
and TryParseExcept
. 也可以使用
ParseExcept
和TryParseExcept
。 but not get succeeded seems return same error 但没有成功似乎返回相同的错误
please give me proper solution. 请给我适当的解决方案。
AndreyAkinshin already explained the real problem. AndreyAkinshin已经解释了真正的问题。 I want to add this as an answer if he lets me..
如果他允许我将其添加为答案。
Both DateTime.Parse
and Convert.ToDatetime
methods uses your CurrentCulture
settings by default. 默认情况下,
DateTime.Parse
和Convert.ToDatetime
方法都使用CurrentCulture
设置。
And your CurrentCulture
can have only one of dd/MM/yyyy
or MM/dd/yyyy
format. 并且您的
CurrentCulture
只能采用dd/MM/yyyy
或MM/dd/yyyy
格式之一。 It can't have both formats as a standard date and time format because it can't know which format it uses when you get a string like 01/01/2014
. 它不能同时使用两种格式作为标准的日期和时间格式,因为当您获得
01/01/2014
类的字符串时,它不知道使用哪种格式。
None of DateTime methods can solve your problem. DateTime方法无法解决您的问题。 Even if you use
DateTime.TryParseExact
overload that takes formats as a string[]
, it parses your string with first successful format that matches in your array. 即使您使用
DateTime.TryParseExact
重载 (将格式作为string[]
,它也会使用与数组中匹配的第一个成功格式来解析您的字符串。
tl;dr tl; dr
You have to know which format of your data has. 您必须知道数据的格式。
If you don't exactly know what is coming in, you have virtually no change of getting the date format in correctly . 如果您不完全知道要输入的内容,那么正确地获取日期格式几乎没有任何改变 。
Take this sample: 拿这个样本:
01/02/2014
Is this the 2nd of January or the 1st of February? 是1月2日还是2月1日?
If you do know the formats, you could use TryParseExact
and just walk down the list until one matches: 如果您确实知道格式,则可以使用
TryParseExact
然后向下浏览列表,直到找到一个匹配项:
DateTime d;
if (DateTime.TryParseExact(encrypt[1], "dd/MM/yyyy", CultureInfo.InvariantCulture, out d))
{ }
else if (DateTime.TryParseExact(encrypt[1], "yyyy/MM/dd", CultureInfo.InvariantCulture, out d))
{ }
You can't tell programatically whether the date is dd/mm/yyyy or mm/dd/yyyy unless they are obviously invalid, eg if you're expecting dd/mm/yyyy and you get 12/14/2014, then that format can only be mm/dd/yyyy. 您无法以编程方式告诉您日期是dd / mm / yyyy还是mm / dd / yyyy,除非它们明显无效,例如,如果您预计dd / mm / yyyy并且得到2014年12月14日,则该格式只能是mm / dd / yyyy。
However, since you are receiving the data from a HTTP request (question is tagged with MVC), you can find the user's culture and use that to parse the date using, eg 但是,由于您正在从HTTP请求接收数据(问题用MVC标记),因此您可以找到用户的区域性,并使用它来解析日期,例如
DateTime.Parse("13/12/2014", new CultureInfo("en-GB")); // Works fine.
DateTime.Parse("13/12/2014", Thread.CurrentThread.CurrentCulture)
See http://msdn.microsoft.com/en-us/library/bb882561(v=vs.110).aspx for more information. 有关更多信息,请参见http://msdn.microsoft.com/zh-cn/library/bb882561(v=vs.110).aspx 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.