Поделиться через


TaiwanCalendar.GetWeekOfYear Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<ComVisibleAttribute(False)> _
Public Overrides Function GetWeekOfYear ( _
    time As DateTime, _
    rule As CalendarWeekRule, _
    firstDayOfWeek As DayOfWeek _
) As Integer
[ComVisibleAttribute(false)]
public override int GetWeekOfYear(
    DateTime time,
    CalendarWeekRule rule,
    DayOfWeek firstDayOfWeek
)

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentOutOfRangeException

time or 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 the time parameter to the last day of the year.

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

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

The CalendarWeekRule property 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, 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 CalendarWeekRule values 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).

using System;
using System.Globalization;


public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

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

      // Displays the total number of weeks in the current year.
      DateTime 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) + "\n";

   }

}

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

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.