c# - Getting error String was not recognized as a valid DateTime -
in gridview showing date on label , fetching date label.
datetime date; label date = gvorderexecuted.rows[0].findcontrol("lbldate") label; date = convert.todatetime(date.text); this code throws error
'string not recognized valid date time'.
i passing value date '3/31/2015'
convert.todatetime method uses datetime.parse method explicitly currentculture settings if don't provide iformatprovider.
looks m/dd/yyyy not standard date , time format of currentculture , that's why throws formatexception.
you can use datetime.parseexact or datetime.tryparseexact methods specify custom string format like;
string s = "3/31/2015"; datetime dt; if(datetime.tryparseexact(s, "m/dd/yyyy", cultureinfo.invariantculture, datetimestyles.none, out dt)) { console.writeline(dt); // 31/03/2015 00:00:00 } "/" separator has special meaning replace me current culture or supplied culture date separator. means, when parse string separator, parsing may fail even if string , format matches exactly. can escape these character '/' without worry it.
string s = "3/31/2015"; datetime dt = datetime.parseexact(s, "m'/'dd'/'yyyy", cultureinfo.currentculture);
Comments
Post a Comment