c# - Why can't DateTime.ParseExact parse DateTime output? -


while struggling datetime.parseexact formatting issues, decided feed parseexact out put datetime.tostring(), this:

datetime date2 = new datetime(1962, 1, 27); string[] expectedformats = { "g", "g", "f", "f", "d", "d", "m/d/yyy", "mm/dd/yyy", "mm-dd-yyy", "mmm dd, yyy", "mmm dd yyy", "mmmm dd, yyy", "mmmm dd yyy" }; bool parsed = false;  foreach (string fmt in expectedformats) {     try     {         datetime dtdatetime = datetime.parseexact(date2.tostring(fmt), fmt, new cultureinfo("en-us"));         parsed = true;     }     catch (exception)     {         parsed = false;     }      console.writeline("[{0}] {1}", parsed,date2.tostring(fmt)); } 

this output:

[true] 1/27/1962 12:00:00 [true] 1/27/1962 12:00 [true] saturday, january 27, 1962 12:00 [true] saturday, january 27, 1962 12:00:00 [true] saturday, january 27, 1962 [true] 1/27/1962 [false] 1/27/1962 [false] 01/27/1962 [false] 01-27-1962 [false] jan 27, 1962 [false] jan 27 1962 [false] january 27, 1962 [false] january 27 1962 

what have parseexact parse custom format strings? wrong expect datetime able ingest it's own output based on same format string?

this demonstrates datetime.parseexact not round-trip safe datetime.tostring. not sure of answer, problem related 3 digit year format yyy. since 1962 cannot represented in 3 digits tostring forced use 4 digits. apparently parseexact not smart enough reverse logic , instead looking 3 digits. workaround use yyyy instead of yyy. submit bug microsoft connect website , see comes of it.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -