How to Convert string to date in c# -
this question has answer here:
- converting string datetime 10 answers
i have string format of date looks "04/16/2014 19:10"
, want convert datetime.
i tried, below codes, didn't work. got error "string not recognized valid datetime."
how convert datetime
datetime dt1 = datetime.parse(datetimestring); datetime dt = system.convert.todatetime(datetimestring);
the problem parse, using it, take account current culture of machine means (depending on are) date interpreted differently.
whenever parsing specific dates should use parseexact or tryparseexact, way leave no room ambiguity on how date should interpreted (regardless of culture)
datetime dt; if (datetime.tryparseexact("04/16/2014 19:10", "mm/dd/yyyy hh:mm", cultureinfo.invariantculture, datetimestyles.none, out dt)) { // date parsed correctly, use `dt` }
Comments
Post a Comment