Click to See Complete Forum and Search --> : DateTime.ParseExact - "day of week was incorrect" exception!!


embalism
January 31st, 2010, 03:33 PM
Greetings,

Perhaps this is a bug with DateTime.ParseExact()?

Input:
SUNDAYSEPTEMBER271:00PMET

Code:
prediction.TimeOfGame = DateTime.ParseExact((_date + timeOfGameNoCommasOrSpaces).ToUpper(),
"ddddMMMMdh:mmttET",
null);

Returns:
System.FormatException: String was not recognized as a valid DateTime because the day of week was incorrect.


I'd like to report this, perhaps someone knows the reason for the exception?

Thanks,
Tom

nelo
January 31st, 2010, 05:35 PM
Hi embalism,

You may want to look at the following things...

1. It looks like you haven't specified the year in the format string. I assume the year is 1971 going by the process of elimination. I might be wrong of course. But I think you need to specify the year if you want to create a valid date time.

2. Secondly once you do specify the year, the numbers have to correspond to a real date in the calendar. For example if you want to create 2nd of September 1971 that date was a Thursday and not a Sunday.

3. Thirdly I would probably change the spelling of "SUNDAY" to "Sunday" to match the DaysOfWeek enumeration. The comparison might be case sensitive (I doubt it though...)

nelo
January 31st, 2010, 05:50 PM
Hi,

Ignore point #3. The case doesn't seem to matter. This worked for me...

var date = DateTime.Format("THURSDAYSeptember02711:00PM", "ddddMMMMddyyh:mmtt", null);