Reputation: 9
I have a datepicker in my c# application that provide strings like these:
"2016/1/1"
"2016/11/15"
But I want to change them to these formats:
"2016/01/01"
"2016/11/15"
I use this code:
string searchstring = " and ProductStartDate Between '" +
String.Format("{0:yyyy/MM/dd}", Convert.ToDateTime(calender_from.Text)) +
"' and '" +
String.Format("{0:yyyy/MM/dd}", Convert.ToDateTime(calender_to.Text)) + "'";
but when I run following error occurs :
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: String was not recognized as a valid DateTime.
Any idea?
Upvotes: 0
Views: 1852
Reputation: 2277
Use DateTime.ParseExact
instead of Convert.ToDateTime
:
calender_from.Text = "2016/1/1";
DateTime date = DateTime.ParseExact(calender_from.Text, "yyyy/M/d", null);
Then you can do this:
string searchstring = " and ProductStartDate Between '" + String.Format("{0:yyyy/MM/dd}", date) + ...
Upvotes: 3