Click here to Skip to main content
15,887,871 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
}
Posted
Updated 9-Nov-16 18:55pm

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!
 
Share this answer
 
try this
C#
DateTime chqdate = DateTime.ParseExact(txtChqDate.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900