Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) 方法

定義

傳回年份中的週,其中包含指定之 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 -> int
Public 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 如何根據 FirstDayOfWeekCalendarWeekRule 所使用的 而有所不同。 如果指定的日期是年份的最後一天, 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 之行事曆的特定文化特性物件包含下列可用於 rulefirstDayOfWeek 參數的文化特性特定值:

注意

這不會完全對應至 ISO 8601。 部落格文章ISO 8601 Year 格式Microsoft .NET會討論差異。 從 .NET Core 3.0 開始, ISOWeek.GetYearISOWeek.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)

對於某些行事曆,即使大於該行事曆 MinSupportedDateTime 屬性所傳回的日期,方法的呼叫 GetWeekOfYear 也會 ArgumentOutOfRangeException 針對 和 firstDayOfWeektime 的特定組合 rule 擲回 。 下表列出受影響的行事曆、特定 rule 值,以及最早支援 time 值的範圍。 特定的最小值 DateTime 取決於 參數的值 firstDayOfWeek

Calendar CalendarWeekRule 值 西曆日期 (M/dd/yyyy) 行事曆中的日期 (M/dd/yyyy)
ChineseLunisolarCalendar FirstFullWeek 2/19/1901 到 2/25/1901 1/1/1901 到 1/7/1901
ChineseLunisolarCalendar FirstFourDayWeek 2/19/1901 到 2/22/1901 1/01/1901 到 1/04/1901
HebrewCalendar FirstDay 9/17/1583 1/01/5344
HebrewCalendar FirstFullWeek 9/17/1583 至 9/23/1583 1/01/5344 至 1/07/5344
HebrewCalendar FirstFourDayWeek 9/17/1583 到 9/20/1583 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/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

適用於

另請參閱