c# - DateTime ParseExact Throws Exception -


well trying parse date

5/10/2013 002704

        var stt = "5/10/2013 002704";         result = datetime.parseexact(stt, "dd-mm-yyyy hhmmss", cultureinfo.invariantculture); 

but exception

string not recognized valid datetime.

however!, code works parse time *without date*

        var stt = "002704";         result = datetime.parseexact(stt, "hhmmss", cultureinfo.invariantculture); 

well hope helps me problem , in advance...

hint : fails

    var stt = "5/10/2013 002704";     result = datetime.parseexact(stt, "dd/mm/yyyy hhmmss", cultureinfo.invariantculture); 

well works! every 1 helped me here his\her nice appreciate much. take xy problem account next time :d.

ah, you're doing parse exact dd passing in d. change input string "05/10/2013 002704", , make sure you're using / in delimiter.

var stt = "05/10/2013 002704"; result = datetime.parseexact(stt, "dd/mm/yyyy hhmmss", cultureinfo.invariantculture); 

edit

sorry, had take phone call , couldn't finish thought. instead of using dd want d. work 05/10/2013 or number 31 (as pointed out in comments on question). also, think @danj made great comment reference use case of method.

the short answer is, if you're going use parse*exact*(), you'd better sure string you're supplying matches format specifier.

if going move forward parseexact should use:

result = datetime.parseexact(stt, "d/mm/yyyy hhmmss", cultureinfo.invariantculture); 

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 -