JapaneseCalendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) JapaneseCalendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) JapaneseCalendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) JapaneseCalendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Method

定义

返回一年中包含指定 DateTime 值中的日期的那个星期。Returns the week of the year that includes the date in the specified DateTime.

public:
 override int GetWeekOfYear(DateTime time, System::Globalization::CalendarWeekRule rule, DayOfWeek firstDayOfWeek);
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetWeekOfYear (DateTime time, System.Globalization.CalendarWeekRule rule, DayOfWeek firstDayOfWeek);
override this.GetWeekOfYear : DateTime * System.Globalization.CalendarWeekRule * DayOfWeek -> int
Public Overrides Function GetWeekOfYear (time As DateTime, rule As CalendarWeekRule, firstDayOfWeek As DayOfWeek) As Integer

参数

time
DateTime DateTime DateTime DateTime

要读取的 DateTimeThe DateTime to read.

rule
CalendarWeekRule CalendarWeekRule CalendarWeekRule CalendarWeekRule

CalendarWeekRule 值之一,用于定义一个日历周。One of the CalendarWeekRule values that defines a calendar week.

firstDayOfWeek
DayOfWeek DayOfWeek DayOfWeek DayOfWeek

DayOfWeek 值之一,用于表示一个星期的第一天。One of the DayOfWeek values that represents the first day of the week.

返回

从 1 开始的一个整数,用于表示一年中包含 time 参数内日期的那个星期。A 1-based integer that represents the week of the year that includes the date in the time parameter.

异常

timefirstDayOfWeek 超出了日历支持的范围。time or firstDayOfWeek is outside the range supported by the calendar.

- 或 --or- rule 不是有效的 CalendarWeekRule 值。rule is not a valid CalendarWeekRule value.

示例

下面的代码示例演示如何根据所使用GetWeekOfYearFirstDayOfWeekCalendarWeekRule值而产生的结果。The following code example shows how the result of GetWeekOfYear varies depending on the FirstDayOfWeek and CalendarWeekRule values used. 如果指定的日期是一年中的最后一天GetWeekOfYear , 则返回该年份的总周数。If the specified date is the last day of the year, GetWeekOfYear returns the total number of weeks in that year.

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

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

注解

此方法可用于通过将time参数设置为一年中的最后一天来确定该年份中的周数。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.

属性包含可用于rulefirstDayOfWeek参数的区域性特定值。 CultureInfo.DateTimeFormatThe CultureInfo.DateTimeFormat property contains culture-specific values that can be used for the rule and firstDayOfWeek parameters.

CultureInfo.DateTimeFormat CalendarFirstDayOfWeek属性包含默认DayOfWeek值, 该值表示特定区域性的一周的第一天, 使用的属性中指定的日历表示该周的第一天。 CultureInfo.DateTimeFormatThe 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.

Calendar CalendarWeekRuleCalendarWeekRule CultureInfo.DateTimeFormat属性包含默认值, 该默认值使用的属性中指定的日历为特定区域性定义日历周。 CultureInfo.DateTimeFormatThe 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.

例如, 在中GregorianCalendar, 1 GetWeekOfYear月1日的方法返回1。For example, in GregorianCalendar, the GetWeekOfYear method for January 1 returns 1.

适用于