GetWeekOfYear Method

Calendar.GetWeekOfYear Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

'Declaration
Public Overridable Function GetWeekOfYear ( _
	time As DateTime, _
	rule As CalendarWeekRule, _
	firstDayOfWeek As DayOfWeek _
) As Integer

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

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


Imports System.Globalization

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim dfi As DateTimeFormatInfo = DateTimeFormatInfo.CurrentInfo
      Dim date1 As Date = #1/1/2011#
      Dim cal As Calendar = dfi.Calendar

      outputBlock.Text += String.Format("{0:d}: Week {1} ({2})", date1, 
                        cal.GetWeekOfYear(date1, dfi.CalendarWeekRule, 
                                          dfi.FirstDayOfWeek),
                        cal.ToString().Substring(cal.ToString().LastIndexOf(".") + 1)) _  
                        & vbCrLf      
   End Sub
End Module
' 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)

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

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


Imports System.Globalization

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' 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.
      outputBlock.Text += String.Format("The CalendarWeekRule used for the en-US culture is {0}.", myCWR) & vbCrLf
      outputBlock.Text += String.Format("The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW) & vbCrLf
      outputBlock.Text += String.Format("Therefore, the current week is Week {0} of the current year.", myCal.GetWeekOfYear(DateTime.Now, myCWR, myFirstDOW)) & vbCrLf

      ' Displays the total number of weeks in the current year.
      Dim LastDay = New System.DateTime(DateTime.Now.Year, 12, 31)
      outputBlock.Text += String.Format("There are {0} weeks in the current year ({1}).", myCal.GetWeekOfYear(LastDay, myCWR, myFirstDOW), LastDay.Year) & vbCrLf
   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).



Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft