次の方法で共有


Calendar.GetWeekOfYear メソッド

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

Public Overridable Function GetWeekOfYear( _
   ByVal time As DateTime, _   ByVal rule As CalendarWeekRule, _   ByVal firstDayOfWeek As DayOfWeek _) As Integer
[C#]
public virtual int GetWeekOfYear(DateTimetime,CalendarWeekRulerule,DayOfWeekfirstDayOfWeek);
[C++]
public: virtual int GetWeekOfYear(DateTimetime,CalendarWeekRulerule,DayOfWeekfirstDayOfWeek);
[JScript]
public function GetWeekOfYear(
   time : DateTime,rule : CalendarWeekRule,firstDayOfWeek : DayOfWeek) : int;

パラメータ

戻り値

time の日付を含む年の週を表す 1 から始まる整数。

例外

例外の種類 条件
ArgumentOutOfRangeException firstDayOfWeek が暦でサポートされている範囲外の値です。

または

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

解説

このメソッドは、その年の最後の日付を time に設定することにより、その年の週の数を確認するために使用できます。

CultureInfo.DateTimeFormat には、 rulefirstDayOfWeek に使用できるカルチャ固有の値が含まれます。

CultureInfo.DateTimeFormatFirstDayOfWeek には、 CultureInfo.DateTimeFormatCalendar プロパティで指定された暦を使用して、特定のカルチャにおける週の最初の曜日を表す既定の DayOfWeek 値が格納されます。

CultureInfo.DateTimeFormatCalendarWeekRule には、 CultureInfo.DateTimeFormatCalendar プロパティを使用して、特定のカルチャの暦における週を定義する既定の CalendarWeekRule 値が格納されます。

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

使用例

[Visual Basic, C#, C++] 使用する FirstDayOfWeekCalendarWeekRule に応じて GetWeekOfYear の結果が変わるようすを示すコードの例を次に示します。指定した日付がその年の最後の日だった場合、 GetWeekOfYear はその年の週の総数を返します。

 
Imports System
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 'Main 
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).


[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).

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main()
{
   // Gets the Calendar instance associated with a CultureInfo.
   CultureInfo* myCI = new CultureInfo(S"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(S"The CalendarWeekRule used for the en-US culture is {0}.", __box(myCWR));
   Console::WriteLine(S"The FirstDayOfWeek used for the en-US culture is {0}.", __box(myFirstDOW));
   Console::WriteLine(S"Therefore, the current week is Week {0} of the current year.",
      __box(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(S"There are {0} weeks in the current year ( {1}).", 
      __box(myCal -> GetWeekOfYear(LastDay, myCWR, myFirstDOW)), __box(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).
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Calendar クラス | Calendar メンバ | System.Globalization 名前空間 | System.DateTime | CalendarWeekRule | System.DayOfWeek | GetEra | GetYear | GetMonth | GetDayOfYear | GetDayOfMonth | GetDayOfWeek | GetHour | GetMinute | GetSecond | GetMilliseconds