Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一年中包含指定 DateTime 值中的日期的那个星期。
public:
 virtual int GetWeekOfYear(DateTime time, System::Globalization::CalendarWeekRule rule, DayOfWeek firstDayOfWeek);public virtual int GetWeekOfYear (DateTime time, System.Globalization.CalendarWeekRule rule, DayOfWeek firstDayOfWeek);abstract member GetWeekOfYear : DateTime * System.Globalization.CalendarWeekRule * DayOfWeek -> int
override this.GetWeekOfYear : DateTime * System.Globalization.CalendarWeekRule * DayOfWeek -> intPublic Overridable Function GetWeekOfYear (time As DateTime, rule As CalendarWeekRule, firstDayOfWeek As DayOfWeek) As Integer参数
- time
- DateTime
日期和时间值。
- rule
- CalendarWeekRule
定义日历周的枚举值。
- firstDayOfWeek
- DayOfWeek
表示一周的第一天的枚举值。
返回
一个正整数,用于表示一年中包含 time 参数内日期所在的星期。
例外
              time 早于 MinSupportedDateTime 或晚于 MaxSupportedDateTime。
- 或 -
              firstDayOfWeek 不是有效的 DayOfWeek 值。
- 或 -
              rule 不是有效的 CalendarWeekRule 值。
示例
下面的代码示例演示 的结果 GetWeekOfYear 如何因 FirstDayOfWeek 使用的 和 CalendarWeekRule 而异。 如果指定的日期是一年中的最后一天, GetWeekOfYear 则 返回该年的总周数。
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
'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 一年中的最后一天来确定一年中的周数。
              DateTimeFormatInfo使用 属性指示DateTimeFormatInfo.Calendar的日历的特定区域性的 对象包括可用于 和 firstDayOfWeek 参数的以下区域性特定值rule:
- 属性 DateTimeFormatInfo.FirstDayOfWeek 包含可用于 - firstDayOfWeek参数的默认一周的第一天。
- 属性 DateTimeFormatInfo.CalendarWeekRule 包含可用于 - rule参数的默认日历周规则。
注意
这不会完全映射到 ISO 8601。 Microsoft .NET 中的博客文章 ISO 8601 Year 周格式中讨论了这些差异。 从 .NET Core 3.0 开始, ISOWeek.GetYear 并 ISOWeek.GetWeekOfYear 解决此问题。
以下示例使用当前区域性的 DateTimeFormatInfo 对象来确定 2011 年 1 月 1 日是公历中每年的第一周。
using System;
using System.Globalization;
public class Example
{
   public static void Main()
   {
      DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
      DateTime date1 = new DateTime(2011, 1, 1);
      Calendar cal = dfi.Calendar;
      Console.WriteLine("{0:d}: Week {1} ({2})", date1,
                        cal.GetWeekOfYear(date1, dfi.CalendarWeekRule,
                                          dfi.FirstDayOfWeek),
                        cal.ToString().Substring(cal.ToString().LastIndexOf(".") + 1));
   }
}
// The example displays the following output:
//       1/1/2011: Week 1 (GregorianCalendar)
Imports System.Globalization
Module Example
   Public Sub Main()
      Dim dfi As DateTimeFormatInfo = DateTimeFormatInfo.CurrentInfo
      Dim date1 As Date = #1/1/2011#
      Dim cal As Calendar = dfi.Calendar
      
      Console.WriteLine("{0:d}: Week {1} ({2})", date1, 
                        cal.GetWeekOfYear(date1, dfi.CalendarWeekRule, 
                                          dfi.FirstDayOfWeek),
                        cal.ToString().Substring(cal.ToString().LastIndexOf(".") + 1))       
   End Sub
End Module
' The example displays the following output:
'       1/1/2011: Week 1 (GregorianCalendar)
对于某些日历,GetWeekOfYear调用 方法会为 和 firstDayOfWeek 值的特定组合rule引发 ,ArgumentOutOfRangeException即使 time 大于该日历的 属性返回的MinSupportedDateTime日期也是如此。 下表列出了受影响的日历、特定 rule 值以及最早支持 time 的值的范围。 特定的最小值 DateTime 取决于 参数的值 firstDayOfWeek 。
| 日历 | CalendarWeekRule 值 | 公历日期 (M/dd/yyyy) | 日历中的日期 (M/dd/yyyy) | 
|---|---|---|---|
| ChineseLunisolarCalendar | FirstFullWeek | 1901/2/19 至 2/25/1901 | 1901/1/1 至 1901/1/7 | 
| ChineseLunisolarCalendar | FirstFourDayWeek | 1901/2/19 至 2/22/1901 | 1901/1/01 至 1901/1/04 | 
| HebrewCalendar | FirstDay | 9/17/1583 | 1/01/5344 | 
| HebrewCalendar | FirstFullWeek | 1583/9/17 至 1583/9/23 | 1/01/5344 到 1/07/5344 | 
| HebrewCalendar | FirstFourDayWeek | 1583/9/17 至 1583/9/20 | 1/01/5344 到 1/04/5344 | 
| HijriCalendar | FirstFullWeek | 7/18/0622 至 7/24/0622 | 1/01/0001 至 1/07/0001 | 
| HijriCalendar | FirstFourDayWeek | 7/18/0622 至 7/21/0622 | 1/01/0001 至 1/04/0001 | 
| JapaneseLunisolarCalendar | FirstFullWeek | 1/28/1960 至 2/03/1960 | 1/01/35 至 1/07/0035 | 
| JapaneseLunisolarCalendar | FirstFourDayWeek | 1/28/1960 至 1/31/1960 | 1/01/0035 至 1/04/0035 | 
| JulianCalendar | FirstFullWeek | 1/01/0001 至 1/05/0001 | 1/03/0001 至 1/07/0001 | 
| JulianCalendar | FirstFourDayWeek | 1/01/0001 到 1/02/0001 | 1/03/0001 至 1/04/0001 | 
| KoreanLunisolarCalendar | FirstFullWeek | 2/14/0918 至 2/20/0918 | 1/01/0918 至 1/07/0918 | 
| KoreanLunisolarCalendar | FirstFourDayWeek | 2/14/0918 至 2/17/0918 | 1/01/0918 至 1/04/0918 | 
| PersianCalendar | FirstFullWeek | 3/21/0622 至 3/27/0622 | 1/01/0001 至 1/07/0001 | 
| PersianCalendar | FirstFourDayWeek | 3/21/0622 至 3/24/0622 | 1/01/0001 至 1/04/0001 | 
| TaiwanLunisolarCalendar | FirstFullWeek | 2/18/1912 至 2/24/1912 | 1/01/0001 至 1/07/0001 | 
| TaiwanLunisolarCalendar | FirstFourDayWeek | 2/18/1912 至 2/21/1912 | 1/01/0001 至 1/04/0001 | 
| UmAlQuraCalendar | FirstFullWeek | 4/30/1900 至 5/06/1900 | 1/01/1318 至 1/07/1318 | 
| UmAlQuraCalendar | FirstFourDayWeek | 4/30/1900 至 5/03/1900 | 1/01/1318 至 1/04/1318 |