Conversion of string to datetime with specific format

1

I have string in the following format: 16Mar2009 (mon) , but I am not able to convert to datetime .

 string format = "dMMMyyyy(dd)";
 DateTime dt = DateTime.ParseExact("16Mar2009(mon)", format,CultureInfo.InvariantCulture);

Returns the following error:

  

String was not recognized as a valid DateTime.

    
asked by anonymous 20.01.2015 / 22:27

1 answer

1

I had a basic format error, a d letter is missing on the day and day of the week. The right thing is this:

DateTime.ParseExact("16Mar2009(mon)", "ddMMMyyyy(ddd)", CultureInfo.InvariantCulture)

See working on dotNetFiddle .

Reference: Custom Date and Time Format Strings

    
20.01.2015 / 22:36