c# - For valid DateTime(Error shows string was not recognized ) -
i trying convert string formated value date type format dd/mm/yyyy. runs fine when enter fromdate(dd/mm/yyyy) in textbox fine , todate(dd/mm/yyyy) in textbox gives error string not recognized valid datetime.what problem dont know. same code run on appliction run fine in application shows error.
below have used array required format , split used.
string fromdate = punchin.tostring(); string[] arrfromdate = fromdate.split('/'); fromdate = arrfromdate[1].tostring() + "/" + arrfromdate[0].tostring() + "/" + arrfromdate[2].tostring(); datetime d1 = datetime.parse(fromdate.tostring());
i got 5/13/2013 12:21:35 pm in string fromdate
use datetime.tryparseexact, don't have split string based on /
, first 3 items array instead can do:
datetime dt; if (datetime.tryparseexact("5/13/2013 12:21:35 pm", "m/d/yyyy hh:mm:ss tt", cultureinfo.invariantculture, datetimestyles.none, out dt)) { //date fine }
using single d
, single m
can accomodate single digit double digits day/month part. you can pass punchin
string parameter, calling tostring
on string types redundant.
Comments
Post a Comment