为什么我收到字符串未被识别为有效的日期时间。 [英] Why I am getting string was not recognized as a valid datetime.

查看:85
本文介绍了为什么我收到字符串未被识别为有效的日期时间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有jquery日期选择器的文本框txtChqDate。如果日期少于今天的日期(2016-11-10)被选中而不是它的罚款但是日期大于今天日期说(2016-11-26)被选中而不是它显示错误字符串未被识别为有效日期时间。



我尝试过:



我用过的代码:



DateTime chqdate = Convert.ToDateTime(txtChqDate.Text);

DateTime transdate = Convert.ToDateTime(txtTransDate.Text);

if(chqdate> transdate)

{

string script =alert('Check Date未到达');;

ClientScript.RegisterClientScriptBlock(this.GetType(),Alert;, script,true);

txtChqDate.Text =

txtAccNo.Focus();

}

其他

{

txtpaidto。焦点();

}

解决方案

它与你的本地日期设置有关...

Convert.ToDateTime()相当于DateTime.Parse(,CultureInfo.CurrentCulture),所以很明显,YYYY-MM-DD不适合你的当地......它接缝是YYYY-DD-MM,因为它试图将26解释为月份失败...

使用 DateTime.ParseExact [ ^ ]方法,格式正确!

试试这个

 DateTime chqdate = DateTime.ParseExact(txtChqDate.Text,  yyyy-MM-dd,CultureInfo.InvariantCulture); 





参考

DateTime.TryParseExact方法(系统) [ ^ ]

DateTime.ParseExact Method(String,String,IFormatProvider) )(系统) [ ^ ]


I have a textbox txtChqDate having jquery date picker.If date less than todays date(2016-11-10) is selected than its fine but date greater than todays date say(2016-11-26) is selected than it shows error "String was not recognized as a valid DateTime".

What I have tried:

Code I have used:

DateTime chqdate = Convert.ToDateTime(txtChqDate.Text);
DateTime transdate = Convert.ToDateTime(txtTransDate.Text);
if (chqdate >transdate)
{
string script ="alert('Cheque Date Not Reached');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert";, script, true);
txtChqDate.Text =""
txtAccNo.Focus();
}
else
{
txtpaidto.Focus();
}

解决方案

It has something to do with your local date settings...
Convert.ToDateTime("") is equivalent to DateTime.Parse("", CultureInfo.CurrentCulture), so it is obvious that the YYYY-MM-DD does not fit your local... It seams to be YYYY-DD-MM, as it try to interpret 26 as month an fails...
Use DateTime.ParseExact[^] method with the proper format!


try this

DateTime chqdate = DateTime.ParseExact(txtChqDate.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);



refer
DateTime.TryParseExact Method (System)[^]
DateTime.ParseExact Method (String, String, IFormatProvider) (System)[^]


这篇关于为什么我收到字符串未被识别为有效的日期时间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆