无法将“获取错误字符串"识别为有效的DateTime.解析时 [英] Getting Error String was not recognized as a valid DateTime. while Parsing
本文介绍了无法将“获取错误字符串"识别为有效的DateTime.解析时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一小段代码来验证项目中的日期格式,并将此错误获取为
字符串未被识别为有效的DateTime.
Hi,
I am using a small piece of code to validate Date Format in my Project and Getting this Error as
String was not recognized as a valid DateTime.
private bool IsValidDate(string date)
{
if (date.Equals(string.Empty))
return false;
try
{
DateTime.Parse(date);
return true;
}
catch
{ return false; }
}
我要传递的是一个字符串,例如("29/09/2010 11:02:50"),这会导致尝试解析以及此错误的发生.
需要帮助..
问候
honeyashu
What I am passing is a String,e.g.("29/09/2010 11:02:50"), which inturns trying to Parse and where this error is coming.
Need help..
Regards
honeyashu
推荐答案
您好,您需要定义Ex所需的日期格式:DDMMMYYYY或
YYYY MM DD等,然后尝试使用下面的代码
Hi You ahve to define which date format is required for Ex : DDMMMYYYY or
YYYY MM DD and so on then try use the code below
DateTime.ParseExact(myString,yyyy-MM-dd, System.Globalization.CultureInfo.InvariantCulture);
将YYY-MM-DD替换为所需的日期格式
replace YYY-MM-DD with the date format required
嘿,这样做
Hey do this
DateTime.ParseExact(dateString,"dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
这篇关于无法将“获取错误字符串"识别为有效的DateTime.解析时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文