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 パラメーターの日付を含む年の週を表す正の整数。

例外

timeMinSupportedDateTime よりも前か、MaxSupportedDateTime よりも後です。

- または -

firstDayOfWeek は有効な DayOfWeek 値ではありません。

- または -

rule は有効な CalendarWeekRule 値ではありません。

次のコード例は、 の結果GetWeekOfYearが、 と CalendarWeekRule で使用される によってどのように異なるFirstDayOfWeekかを示しています。 指定した日付が年の最後の日である場合は、 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含まれています。

注意

これは ISO 8601 に正確にはマップされません。 違いについては、ブログの「ISO 8601 Week of Year」の形式 (Microsoft .NET) で説明されています。 .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呼び出しでは、 がカレンダーの rule プロパティによって返される日付より大きい場合timeでも、 と firstDayOfWeek 値の特定のMinSupportedDateTime組み合わせに対して がスローArgumentOutOfRangeExceptionされます。 次の表に、影響を受けるカレンダー、特定 rule の値、サポートされている time 最も古い値の範囲を示します。 特定の最小値 DateTime は、 パラメーターの firstDayOfWeek 値によって異なります。

Calendar CalendarWeekRule 値 グレゴリオ暦の日付 (M/dd/yyyy) カレンダーの日付 (M/dd/yyyy)
ChineseLunisolarCalendar FirstFullWeek 1901 年 2 月 19 日から 1901 年 2 月 25 日 1901 年 1 月 1 日から 1901 年 1 月 7 日
ChineseLunisolarCalendar FirstFourDayWeek 1901 年 2 月 19 日から 1901 年 2 月 22 日 1901 年 1 月 1 日から 1901 年 4 月 1 日
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 1960 年 1 月 28 日から 1960 年 2 月 3 日 1/01/35 から 1/07/0035
JapaneseLunisolarCalendar FirstFourDayWeek 1960 年 1 月 28 日から 1960 年 1 月 31 日 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 1912 年 2 月 18 日から 1912 年 2 月 24 日 1/01/0001 から 1/07/0001
TaiwanLunisolarCalendar FirstFourDayWeek 1912 年 2 月 18 日から 1912 年 2 月 21 日 1/01/0001 から 1/04/0001
UmAlQuraCalendar FirstFullWeek 1900 年 4 月 30 日から 1900 年 5 月 6 日 1/01/1318 から 1/07/1318
UmAlQuraCalendar FirstFourDayWeek 1900 年 4 月 30 日から 1900 年 5 月 3 日 1/01/1318 から 1/04/1318

適用対象

こちらもご覧ください