Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Calendar.GetWeekOfYear Method

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

[Visual Basic]
Public Overridable Function GetWeekOfYear( _
   ByVal time As DateTime, _
   ByVal rule As CalendarWeekRule, _
   ByVal firstDayOfWeek As DayOfWeek _
) As Integer
[C#]
public virtual int GetWeekOfYear(
 DateTime time,
 CalendarWeekRule rule,
 DayOfWeek firstDayOfWeek
);
[C++]
public: virtual int GetWeekOfYear(
 DateTime time,
 CalendarWeekRule rule,
 DayOfWeek firstDayOfWeek
);
[JScript]
public function GetWeekOfYear(
   time : DateTime,
 rule : CalendarWeekRule,
 firstDayOfWeek : DayOfWeek
) : int;

Parameters

time
The DateTime to read.
rule
A CalendarWeekRule value that defines a calendar week.
firstDayOfWeek
A DayOfWeek value that represents the first day of the week.

Return Value

A 1-based integer that represents the week of the year that includes the date in time.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException firstDayOfWeek is outside the range supported by the calendar.

-or-

rule is not a valid CalendarWeekRule value.

Remarks

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

CultureInfo.DateTimeFormat contains culture-specific values that can be used for rule and firstDayOfWeek.

FirstDayOfWeek of CultureInfo.DateTimeFormat contains the default DayOfWeek value that represents the first day of the week for a specific culture, using the calendar specified in the Calendar property of CultureInfo.DateTimeFormat.

CalendarWeekRule of CultureInfo.DateTimeFormat contains the default CalendarWeekRule value that defines a calendar week for a specific culture, using the calendar specified in the Calendar property of CultureInfo.DateTimeFormat.

For example, in GregorianCalendar, GetWeekOfYear for January 1 returns 1.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Imports System
Imports System.Globalization

Public Class SamplesCalendar

   Public Shared Sub Main()
      
      ' Gets the Calendar instance associated with a CultureInfo.
      Dim myCI As New CultureInfo("en-US")
      Dim myCal As Calendar = myCI.Calendar
      
      ' Gets the DTFI properties required by GetWeekOfYear.
      Dim myCWR As CalendarWeekRule = myCI.DateTimeFormat.CalendarWeekRule
      Dim myFirstDOW As DayOfWeek = 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.
      Dim 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)
   End Sub 'Main 
End Class 'SamplesCalendar


'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).


[C#] 
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).

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main()
{
   // Gets the Calendar instance associated with a CultureInfo.
   CultureInfo* myCI = new CultureInfo(S"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(S"The CalendarWeekRule used for the en-US culture is {0}.", __box(myCWR));
   Console::WriteLine(S"The FirstDayOfWeek used for the en-US culture is {0}.", __box(myFirstDOW));
   Console::WriteLine(S"Therefore, the current week is Week {0} of the current year.",
      __box(myCal -> GetWeekOfYear(DateTime::Now, myCWR, myFirstDOW)));

   // Displays the total number of weeks in the current year.
   DateTime LastDay = System::DateTime(DateTime::Now.Year, 12, 31);
   Console::WriteLine(S"There are {0} weeks in the current year ( {1}).", 
      __box(myCal -> GetWeekOfYear(LastDay, myCWR, myFirstDOW)), __box(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).
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Calendar Class | Calendar Members | System.Globalization Namespace | System.DateTime | CalendarWeekRule | System.DayOfWeek | GetEra | GetYear | GetMonth | GetDayOfYear | GetDayOfMonth | GetDayOfWeek | GetHour | GetMinute | GetSecond | GetMilliseconds

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.