.NET Framework Class Library
Calendar..::.GetWeekOfYear Method

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

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Overridable Function GetWeekOfYear ( _
    time As DateTime, _
    rule As CalendarWeekRule, _
    firstDayOfWeek As DayOfWeek _
) As Integer
Visual Basic (Usage)
Dim instance As Calendar
Dim time As DateTime
Dim rule As CalendarWeekRule
Dim firstDayOfWeek As DayOfWeek
Dim returnValue As Integer

returnValue = instance.GetWeekOfYear(time, _
    rule, firstDayOfWeek)
C#
public virtual int GetWeekOfYear(
    DateTime time,
    CalendarWeekRule rule,
    DayOfWeek firstDayOfWeek
)
Visual C++
public:
virtual int GetWeekOfYear(
    DateTime time, 
    CalendarWeekRule rule, 
    DayOfWeek firstDayOfWeek
)
JScript
public function GetWeekOfYear(
    time : DateTime, 
    rule : CalendarWeekRule, 
    firstDayOfWeek : DayOfWeek
) : int

Parameters

time
Type: System..::.DateTime
The DateTime to read.
rule
Type: System.Globalization..::.CalendarWeekRule
A CalendarWeekRule value that defines a calendar week.
firstDayOfWeek
Type: System..::.DayOfWeek
A DayOfWeek 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.
Exceptions

ExceptionCondition
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.

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

The FirstDayOfWeek property 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.

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

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

Examples

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

*/
Visual C++
using namespace System;
using namespace System::Globalization;
int main()
{

   // Gets the Calendar instance associated with a CultureInfo.
   CultureInfo^ myCI = gcnew 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 = 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).
*/
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Tags :


Community Content

jonelf
Not ISO 8601 week number compatible
Be aware that this does not return ISO 8601 week numbers.


 System.Globalization.CultureInfo myCI = System.Threading.Thread.CurrentThread.CurrentCulture;

int weekNo = myCI.Calendar.GetWeekOfYear(new DateTime(2008,12,31),

myCI.DateTimeFormat.CalendarWeekRule,

myCI.DateTimeFormat.FirstDayOfWeek);


In Sweden we use ISO 8601 week numbers but even though the culture is set to "sv-SE", CalendarWeekRule is FirstFourDayWeek, and FirstDayOfWeek is Monday the weekNo will be set to 53 instead of the correct 1 with the above code.
I have only tried this with Swedish settings but my guess is that all countries (Austria, Germany, Switzerland and more) using ISO 8601 week numbers will be affected by this problem.

Tags : contentbug

bjarni
its an official bug from 2007

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=276185

Here is my version of the hack

Dim oCultureInfo As New System.Globalization.CultureInfo("sv-SE")
Dim iFirstDayOfWeek = oCultureInfo.DateTimeFormat.FirstDayOfWeek
Function GetWeekOfYearIso8601(ByVal oDate As DateTime) As Integer
Dim oCal As System.Globalization.Calendar = New System.Globalization.GregorianCalendar
Dim day As DayOfWeek = oCal.GetDayOfWeek(oDate)
If day >= DayOfWeek.Monday AndAlso day <= DayOfWeek.Wednesday Then
oDate = oDate.AddDays(3)
End If
' Return the week of our adjusted day
Return oCal.GetWeekOfYear(oDate, oCultureInfo.DateTimeFormat.CalendarWeekRule, iFirstDayOfWeek)
End Function


more about the hack is to be found here !
http://blogs.msdn.com/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net.aspx

Tags :

Page view tracker