英語で読む

次の方法で共有


TaiwanCalendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) メソッド

定義

指定した DateTime の日付を含むその年の週を返します。

C#
public override int GetWeekOfYear (DateTime time, System.Globalization.CalendarWeekRule rule, DayOfWeek firstDayOfWeek);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetWeekOfYear (DateTime time, System.Globalization.CalendarWeekRule rule, DayOfWeek firstDayOfWeek);

パラメーター

time
DateTime

読み取る対象の DateTime

rule
CalendarWeekRule

暦の週を定義するいずれかの CalendarWeekRule 値。

firstDayOfWeek
DayOfWeek

週の最初の曜日を表すいずれかの DayOfWeek 値。

戻り値

time パラメーターの日付を含む年の週を表す正の整数。

属性

例外

time または firstDayOfWeek が暦でサポートされている範囲外の値です。

- または -

rule は正しい CalendarWeekRule 値ではありません。

次のコード例は、 のGetWeekOfYear結果が、使用される と CalendarWeekRuleFirstDayOfWeek値によってどのように異なるかを示しています。 指定した日付が年の最後の日である場合は、 GetWeekOfYear その年の週の合計数を返します。

C#
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).

*/

注釈

このメソッドを使用すると、 パラメーターを年の最後の日に設定 time することで、年の週数を確認できます。

CultureInfo.DateTimeFormatプロパティには、 パラメーターと firstDayOfWeek パラメーターに使用できるカルチャ固有の値がrule含まれています。

の プロパティにはFirstDayOfWeek、 の プロパティで指定されたカレンダーを使用して、特定のカルチャの週の最初のCultureInfo.DateTimeFormat曜日を表す既定値DayOfWeekCalendarCultureInfo.DateTimeFormatまれています。

の プロパティにはCalendarWeekRule、 の プロパティCultureInfo.DateTimeFormatで指定されたカレンダーを使用して、特定のカルチャのカレンダー週を定義する既定値CalendarWeekRuleCalendarCultureInfo.DateTimeFormatまれています。

たとえば、 では GregorianCalendar、1 月 1 日の GetWeekOfYear メソッドは 1 を返します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0