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.

Calendar Class

Software that uses dates sometimes allows those dates to be represented using week numbers. These are numbers between one and fifty-three that describe the number of weeks passed since the start of the year. The rules for calculating a week number are not fixed. In some cases the first day of January is defined as the first day of week one. For other rules 1 January may be part of week 52 or 53 of the previous year.

Luckily, you should not need to create an algorithm to calculate the week number for a date unless the rules that you are using are obscure. The Calendar class in the .NET framework includes a method named, "GetWeekOfYear", that returns a week number based upon a rule set that you can specify. The Calendar class is found in the System.Globalization namespace, as it includes functionality that varies according to the user's culture settings.

using System.Globalization;

Obtaining a Calendar Instance

The constructor for the Calendar class is protected so we cannot instantiate a Calendar object using the new keyword. Instead, we can use the Calendar property of a CultureInfo object. To obtain a CultureInfo object we can use static properties of the class. For example, to get a CultureInfo object that does not include rules that are local to the user we can use CultureInfo.InvariantCulture. In this case we will use the current user's local culture rules by reading the Calendar property of CultureInfo.CurrentCulture.

Calendar calendar = CultureInfo.CurrentCulture.Calendar;

Calculating the Week Number

Now that we have a Calendar object we can obtain the week number for a date. The GetWeekOfYear method's syntax is:

result = calendar.GetWeekOfYear(time, rule, firstDayOfWeek)

After calling the method, result will hold the week number. This is an integer value. In the syntax, calendar is our calendar object and time is a DateTime value that holds the date for which we wish to calculate a week number. These three items are fairly self-explanatory.

The rule parameter accepts a value from the CalendarWeekRule enumeration. This value is used to specify the rules that will be used when calculating the week number. Three options are permitted:

  • FirstDay. When set to this value, the first day of January will always be in week one. Further week numbers are controlled by the third parameter, firstDayOfWeek. This is a DayOfWeek value. The first date after 1 January that falls on the specified day begins week two. In most cases this means that week one is shorter than seven days. For example, 1 January 2012 is a Sunday. Using the FirstDay rule, this date is in week one. If the firstDayOfWeek argument is set to Monday then 2 January 2012 is the start of week two.
  • FirstFullWeek. When the second argument is set to this value, week one begins of the first day of the year that matches the firstDayOfWeek parameter. Dates before the start of week one are in the final week of the previous year. For example, if the cut-off weekday is Monday, 2 January 2012 is in week one and 1 January 2012 is in week 52 of 2011.
  • FirstFourDayWeek. With this rule, week one is the first week that contains four or more days that ends on the day before the cut-off day specified. Week two starts on the specified day of the week. If there are fewer than four days before the first occurrence of the weekday, these are classed as being in the last week of the previous year. If there are four, five of six days before the first specified weekday, these are in week one.

FirstDay Calculation Examples

The following code shows some sample week numbers using the FirstDay rule. Note that 1 January is in week one and the week number changes on the first Tuesday.

static void Main()
{
    Calendar calendar = CultureInfo.CurrentCulture.Calendar;
    DateTime jan1 = new DateTime(2012, 1, 1);
    DateTime jan7 = new DateTime(2012, 1, 7);

    for (DateTime time = jan1; time <= jan7; time = time.AddDays(1))
    {
        int week = calendar.GetWeekOfYear(
            time, CalendarWeekRule.FirstDay, DayOfWeek.Tuesday);
        ShowDateAndWeek(time, week);
    }
}

static void ShowDateAndWeek(DateTime time, int week)
{
    Console.WriteLine("{0} : Week {1}", time.ToString("ddd d MMM yyyy"), week);
}

/* OUTPUT

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

*/
19 December 2011