How to Convert string to date in c# -


this question has answer here:

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

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -