0 out of 3 rated this helpful - Rate this topic

Calendar.GetWeekOfYear Method

Updated: August 2010

Returns the week of the year that includes the date in the specified DateTime value.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
public virtual int GetWeekOfYear(
	DateTime time,
	CalendarWeekRule rule,
	DayOfWeek firstDayOfWeek
)

Parameters

time
Type: System.DateTime
A date and time value.
rule
Type: System.Globalization.CalendarWeekRule
An enumeration value that defines a calendar week.
firstDayOfWeek
Type: System.DayOfWeek
An enumeration value that represents the first day of the week.

Return Value

Type: System.Int32
A positive integer that represents the week of the year that includes the date in the time parameter.
Exception Condition
ArgumentOutOfRangeException

time is earlier than MinSupportedDateTime or later than MaxSupportedDateTime.

-or-

firstDayOfWeek is not a valid DayOfWeek value.

-or-

rule is not a valid CalendarWeekRule value.

This method can be used to determine the number of weeks in the year by setting time to the last day of the year.

The DateTimeFormatInfo object for a particular culture that uses the calendar indicated by the DateTimeFormatInfo.Calendar property includes the following culture-specific values that can be used for the rule and firstDayOfWeek parameters:

The following example uses the current culture's DateTimeFormatInfo object to determine that January 1, 2011 is in the first week of the year in the Gregorian calendar.


using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
      DateTime date1 = new DateTime(2011, 1, 1);
      Calendar cal = dfi.Calendar;

      Console.WriteLine("{0:d}: Week {1} ({2})", date1, 
                        cal.GetWeekOfYear(date1, dfi.CalendarWeekRule, 
                                          dfi.FirstDayOfWeek),
                        cal.ToString().Substring(cal.ToString().LastIndexOf(".") + 1));       
   }
}
// The example displays the following output:
//       1/1/2011: Week 1 (GregorianCalendar)


For some calendars, a call to the GetWeekOfYear method throws an ArgumentOutOfRangeException for particular combinations of rule and firstDayOfWeek values even if time is greater than the date returned by that calendar's MinSupportedDateTime property. The following table lists the affected calendars, the specific rule values, and the range of the earliest supported time values. The specific minimum DateTime value depends on the value of the firstDayOfWeek parameter.

Calendar

CalendarWeekRule value

Gregorian date (M/dd/yyyy)

Date in calendar (M/dd/yyyy)

ChineseLunisolarCalendar

FirstFullWeek

2/19/1901 to 2/25/1901

1/1/1901 to 1/7/1901

ChineseLunisolarCalendar

FirstFourDayWeek

2/19/1901 to 2/22/1901

1/01/1901 to 1/04/1901

HebrewCalendar

FirstDay

9/17/1583

1/01/5344

HebrewCalendar

FirstFullWeek

9/17/1583 to 9/23/1583

1/01/5344 to 1/07/5344

HebrewCalendar

FirstFourDayWeek

9/17/1583 to 9/20/1583

1/01/5344 to 1/04/5344

HijriCalendar

FirstFullWeek

7/18/0622 to 7/24/0622

1/01/0001 to 1/07/0001

HijriCalendar

FirstFourDayWeek

7/18/0622 to 7/21/0622

1/01/0001 to 1/04/0001

JapaneseLunisolarCalendar

FirstFullWeek

1/28/1960 to 2/03/1960

1/01/35 to 1/07/0035

JapaneseLunisolarCalendar

FirstFourDayWeek

1/28/1960 to 1/31/1960

1/01/0035 to 1/04/0035

JulianCalendar

FirstFullWeek

1/01/0001 to 1/05/0001

1/03/0001 to 1/07/0001

JulianCalendar

FirstFourDayWeek

1/01/0001 to 1/02/0001

1/03/0001 to 1/04/0001

KoreanLunisolarCalendar

FirstFullWeek

2/14/0918 to 2/20/0918

1/01/0918 to 1/07/0918

KoreanLunisolarCalendar

FirstFourDayWeek

2/14/0918 to 2/17/0918

1/01/0918 to 1/04/0918

PersianCalendar

FirstFullWeek

3/21/0622 to 3/27/0622

1/01/0001 to 1/07/0001

PersianCalendar

FirstFourDayWeek

3/21/0622 to 3/24/0622

1/01/0001 to 1/04/0001

TaiwanLunisolarCalendar

FirstFullWeek

2/18/1912 to 2/24/1912

1/01/0001 to 1/07/0001

TaiwanLunisolarCalendar

FirstFourDayWeek

2/18/1912 to 2/21/1912

1/01/0001 to 1/04/0001

UmAlQuraCalendar

FirstFullWeek

4/30/1900 to 5/06/1900

1/01/1318 to 1/07/1318

UmAlQuraCalendar

FirstFourDayWeek

4/30/1900 to 5/03/1900

1/01/1318 to 1/04/1318

The following code example shows how the result of GetWeekOfYear varies depending on the FirstDayOfWeek and the CalendarWeekRule used. If the specified date is the last day of the year, GetWeekOfYear returns the total number of weeks in that year.


using System;
using System.Globalization;


public class SamplesCalendar  {

   public static void Main()  {

      // Gets the Calendar instance associated with a CultureInfo.
      CultureInfo myCI = new CultureInfo("en-US");
      Calendar myCal = myCI.Calendar;

      // Gets the DTFI properties required by GetWeekOfYear.
      CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
      DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;

      // Displays the number of the current week relative to the beginning of the year.
      Console.WriteLine( "The CalendarWeekRule used for the en-US culture is {0}.", myCWR );
      Console.WriteLine( "The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW );
      Console.WriteLine( "Therefore, the current week is Week {0} of the current year.", myCal.GetWeekOfYear( DateTime.Now, myCWR, myFirstDOW ));

      // Displays the total number of weeks in the current year.
      DateTime LastDay = new System.DateTime( DateTime.Now.Year, 12, 31 );
      Console.WriteLine( "There are {0} weeks in the current year ({1}).", myCal.GetWeekOfYear( LastDay, myCWR, myFirstDOW ), LastDay.Year );

   }

}

/*
This code produces the following output.  Results vary depending on the system date.

The CalendarWeekRule used for the en-US culture is FirstDay.
The FirstDayOfWeek used for the en-US culture is Sunday.
Therefore, the current week is Week 1 of the current year.
There are 53 weeks in the current year (2001).

*/


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

August 2010

Noted that in some cases an exception can be thrown for dates later than MinSupportedDateTime.

Content bug fix.

June 2010

Added a discussion of DateTimeFormatInfo properties and an example.

Information enhancement.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Calendar.GetWeekOfYear() Method
  using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
            DateTimeFormatInfo dfi = new DateTimeFormatInfo();
DateTime date1 = new DateTime(2011, 6, 10);
System.Globalization.Calendar cal = dfi.Calendar;

Console.WriteLine("{0}", cal.GetWeekOfYear(date1, CalendarWeekRule.FirstFullWeek, dfi.FirstDayOfWeek).ToString()); } }

//using example above can show the week number + 1. The above shows the week number!
//shows week 26


Calendar.GetWeekOfYear does not comply with ISO860

Calendar.GetWeekOfYear(new DateTime(2008,12,31), dfi.CalendarWeekRule, dfi.FirstDayOfWeek) returns 53 . This should be week 1 of 2009...

For a workaround I found this online : http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/2008-11/msg01796.html

Bug with week number

DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek = Sunday
DateTimeFormatInfo.CurrentInfo.CalendarWeekRule = FirstDay

CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dateTime,dfi.CalendarWeekRule, dfi.FirstDayOfWeek) returns

for {1/2/2010 12:00:00 AM} - 1st week
for {12/27/2009 12:00:00 AM} - 53rd week.

That's weird.

Windows 2003, Outlook 2007, .NET 3.5 SP1.

Not a Bug: This Is the Expected Behavior


CalendarWeekRule.FirstDay means that January 1 will always be the first day of the week, and that there will be 53 weeks in a year. The number of days in the first week will be the number of days from the January 1 to the first day of the week as defined by the calendar week rule. If the year begins on the first day of the week as defined by the calendar week rule, the first week will have 7 days. For non-leap years, if the first week has 7 days, the last week will have 1 day; otherwise, the number of days in the last week will equal 1 + (7 - daysInFirstWeek). In this case, January 1, 2010, was a Friday, so the first week of 2010 was January 1-January 2. In 2009, January 1 was a Thursday, so the first week of 2009 was January 1-January 3. Because the first week had 3 days, the last week (week 53) had 5 days, from 12/27/2009 to 12/31/2009.

You can determine the date ranges of each week of the year using code like the following. The parameters can be modified to see how other calendar week rules and first days of the week affect the date ranges of weeks and the number of weeks in the year. Here is the Visual Basic code:

Dim startDate As Date = #1/1/2009#
Dim cal As Calendar = CultureInfo.CurrentCulture.Calendar
Dim thisWeek, currentWeek As Integer
Dim thisDay As DateTime = startDate
Do
thisWeek = cal.GetWeekOfYear(thisDay, CalendarWeekRule.FirstDay, DayOfWeek.Sunday)
If thisWeek > currentWeek Or thisWeek < currentWeek Then
If currentWeek > 0 Then Console.WriteLine("{0:d}", thisDay.AddDays(-1))
currentWeek = thisweek
Console.Write("{0}: {1:d} - ", currentWeek, thisDay)
End If
thisDay = thisDay.AddDays(1)
Loop While thisDay <= #1/10/2010#


And here is the C# code:

DateTime startDate = new DateTime(2009, 1, 1);
DateTime endDate = new DateTime(2010, 1, 10);
Calendar cal = CultureInfo.CurrentCulture.Calendar;
int thisWeek = 0;
int currentWeek = 0;

DateTime thisDay = startDate;
do {
thisWeek = cal.GetWeekOfYear(thisDay, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
if (thisWeek > currentWeek || thisWeek < currentWeek) {
if (currentWeek > 0) Console.WriteLine("{0:d}", thisDay.AddDays(-1));
currentWeek = thisWeek;
Console.Write("{0}: {1:d} - ", currentWeek, thisDay);
}
thisDay = thisDay.AddDays(1);
} while (thisDay <= endDate);


--Ron Petrusha
CLR Developer Content Team
Microsoft Corporation