我收到的错误是:
mscorlib.dll中出现"System.FormatException"类型的异常,但未在用户代码中处理
附加信息:字符串未被识别为有效的DateTime.
这是我的代码:
if (txtRefDate.Text != "")
{
string[] splitdate = txtRefDate.Text.Split('-');
string newdate = splitdate[1] + "-" + splitdate[0] + "-" + splitdate[2];
DateTime Compdate = Convert.ToDateTime(newdate);//On this line i'm getting error
string date = Compdate.ToString("yyyy-MM-dd");
obj.RefrenceDate = Convert.ToDateTime(date);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用CalenderExtender我的textbox.我试过Parse和ParseExact,但它不工作.我在这做错了什么?
DateTime Compdate;
if (!String.IsNullOrEmpty(txtRefDate.Text) && DateTime.TryParse(txtRefDate.Text, out Compdate))
{
obj.RefrenceDate = Compdate.Date;
}
Run Code Online (Sandbox Code Playgroud)
另外,请看一下TryParseExact(),它允许您提供有关输入预期格式的更多信息.
如果失败,则开始记录失败的字符串值.我们需要看一些不起作用的例子.