asp.net - How to convert any formatted text box date to DateTime(YYYY-mm-dd)format -
i working date function.data comes in different format like
1."2016-01-31"
2."31-01-2016"
3."31/01/2016"
4."2016/01/31"
5."jan 01 2016"
6."2016-01-02 12:00 am"
now want convert above mentioned format date datetime format yyyy-mm-dd.
i tried many methods format shows error message like
'string not correct format'
'unable convert string value datetime'
i tried,
datetime date=datetime.parseexact(txtdate.text,'yyyy-mm-dd',cultureinfo.invariantculture);
how can cast/convert value datetime format(yyyy-mm-dd) format text box value.
mostly geting error afteri upload server(godaddy , microsoft azure).
can use this
string datetimes=txtdate.text;
string[] datetimes = new string[] { "yyyy-mm-dd", "dd-mm-yyyy","mm/dd/yyyy","yyyy/mm/dd"};
your code isn't quite right. date format string should surrounded " (double quotes) , casing should "yyyy-mm-dd". cultureinfo.invariantculture
spelt incorrectly.
having said that, shouldn't need use parseexact
function convert string datetime
format. can use convert.todatetime
here's sample code used test convert.datetime
function:
string[] datetimes = new string[] { "2010-02-01", "02-03-2011", "03/04/2012", "2013/05/04", "june 05 2014", "2015-07-06 11:00 am" }; stringbuilder sb = new stringbuilder(); foreach (string date in datetimes) { datetime dt = convert.todatetime(date); sb.appendline(dt.tostring("yyyy-mm-dd", cultureinfo.invariantculture)); } string converteddatetimes = sb.tostring();
using relevant parts above, can change code following:
try { datetime date = convert.todatetime(txtdate.text.trim()); string datestring = date.tostring("yyyy-mm-dd", cultureinfo.invariantculture); } catch (formatexception ex) { //handle exception. e.g. show message or print form. }
Comments
Post a Comment