A bug, you say?

There’s a silly bug in a C# method for the Calendar class, GetWeekOfYear: Its second parameter accepts a CalendarWeekRule enumeration with a choice of FirstDay, FirstFourDayWeek or FirstFullWeek. There’s a great example in the API/documentation, however, the samples don’t seem to reflect North American date standards, where Sunday is generally the first day of a week.

Confused? Let me try that again: Take for example the 31st of December of last year. That day fell on a Sunday and that day should be part of the first week of 2007 and not (as the method above consequently will return) part of week 53 of 2006. There’s no week 53 because our week starts with Sunday.

To get around this, you’ll end up filtering the last days of a year you want to be part of the new year. Easy but not the most pretty solution.

This entry was posted in Programming. Bookmark the permalink.