c# - Convert date in string to DateTime with same format -


i have string has date stored in it.

string date = "03-05-2013 00:00:00"; 

i parsed datetime follows:

datetime start = datetime.parse(date); 

start.tostring() gave me "3/5/2013 12:0:00 am"

i used:

datetime start = datetime.parseexact(date,"dd-mm-yyyy hh:mm:ss",cultureinfo.invariantculture); 

then, start.tostring() gave me "3/5/2013 12:0:00 am", exact same result previous one. need keep original formatting. how may it? thanks.

the format parse not dictate how datetime formatted when convert date string. when call tostring on date pulls format current culture of thread code executing on (which defaults culture of machine on).

you can override passing format tostring() i.e.

start.tostring("dd-mm-yyyy hh:mm:ss", cultureinfo.invariantculture); 

see custom date , time formats.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -