BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

Obtaining the Week Number for a Date

Accounting software, time-keeping utilities and calendar applications often present dates using week numbers that commence at the beginning of the year. Using the .NET framework's Calendar class a week number can be calculated using a number of rules.

FirstFullWeek Calculation Examples

To show some examples of the FirstFullWeek rule, modify the week calculation as shown below and run the code again. We are using Tuesday as the cut-off weekday as before. As the year begins on a Sunday, the days before the first Tuesday are in the final week of the previous year.

int week = calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Tuesday);

/* OUTPUT

Sun 1 Jan 2012 : Week 52
Mon 2 Jan 2012 : Week 52
Tue 3 Jan 2012 : Week 1
Wed 4 Jan 2012 : Week 1
Thu 5 Jan 2012 : Week 1
Fri 6 Jan 2012 : Week 1
Sat 7 Jan 2012 : Week 1

*/

If 1 January occurs on the specified day of the week, the first seven days of the calendar year are in week one.

int week = calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday);

/* OUTPUT

Sun 1 Jan 2012 : Week 1
Mon 2 Jan 2012 : Week 1
Tue 3 Jan 2012 : Week 1
Wed 4 Jan 2012 : Week 1
Thu 5 Jan 2012 : Week 1
Fri 6 Jan 2012 : Week 1
Sat 7 Jan 2012 : Week 1

*/

FirstFourDayWeek Calculation Examples

Finally, let's see some examples of the FirstFourDayWeek rule. For the first examples, there are four days before the first Thursday in the year. These four days are in week one and week two starts on the Thursday.

int week = calendar.GetWeekOfYear(
    time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Thursday);

/* OUTPUT

Sun 1 Jan 2012 : Week 1
Mon 2 Jan 2012 : Week 1
Tue 3 Jan 2012 : Week 1
Wed 4 Jan 2012 : Week 1
Thu 5 Jan 2012 : Week 2
Fri 6 Jan 2012 : Week 2
Sat 7 Jan 2012 : Week 2

*/

There are only three days before the first Wednesday of 2012. This is not enough to permit week one to start so setting the cut-off day to Wednesday means that the first three days of 2012 become week 53 of 2011.

int week = calendar.GetWeekOfYear(
    time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Wednesday);

/* OUTPUT

Sun 1 Jan 2012 : Week 53
Mon 2 Jan 2012 : Week 53
Tue 3 Jan 2012 : Week 53
Wed 4 Jan 2012 : Week 1
Thu 5 Jan 2012 : Week 1
Fri 6 Jan 2012 : Week 1
Sat 7 Jan 2012 : Week 1

*/
19 December 2011