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?
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) + ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.