Freigeben über


Calendar.GetWeekOfYear-Methode

Gibt die Woche des Jahres zurück, in die das Datum in der angegebenen DateTime fällt.

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Function GetWeekOfYear ( _
    time As DateTime, _
    rule As CalendarWeekRule, _
    firstDayOfWeek As DayOfWeek _
) As Integer
'Usage
Dim instance As Calendar
Dim time As DateTime
Dim rule As CalendarWeekRule
Dim firstDayOfWeek As DayOfWeek
Dim returnValue As Integer

returnValue = instance.GetWeekOfYear(time, rule, firstDayOfWeek)
public virtual int GetWeekOfYear (
    DateTime time,
    CalendarWeekRule rule,
    DayOfWeek firstDayOfWeek
)
public:
virtual int GetWeekOfYear (
    DateTime time, 
    CalendarWeekRule rule, 
    DayOfWeek firstDayOfWeek
)
public int GetWeekOfYear (
    DateTime time, 
    CalendarWeekRule rule, 
    DayOfWeek firstDayOfWeek
)
public function GetWeekOfYear (
    time : DateTime, 
    rule : CalendarWeekRule, 
    firstDayOfWeek : DayOfWeek
) : int

Parameter

  • firstDayOfWeek
    Ein DayOfWeek-Wert, der den ersten Tag der Woche darstellt.

Rückgabewert

Eine positive ganze Zahl, die die Woche des Jahres darstellt, in die das im time-Parameter angegebene Datum fällt.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentOutOfRangeException

firstDayOfWeek liegt außerhalb des Bereichs, der vom Kalender unterstützt wird.

- oder -

rule ist kein gültiger CalendarWeekRule-Wert.

Hinweise

Mithilfe dieser Methode kann die Anzahl der Wochen im Jahr bestimmt werden, indem time auf den letzten Tag des Jahres festgelegt wird.

Die CultureInfo.DateTimeFormat-Eigenschaft enthält kulturspezifische Werte, die für den rule-Parameter und den firstDayOfWeek-Parameter verwendet werden können.

Die FirstDayOfWeek-Eigenschaft von CultureInfo.DateTimeFormat enthält den DayOfWeek-Standardwert, der den ersten Tag der Woche für eine bestimmte Kultur darstellt. Hierbei wird der in der Calendar-Eigenschaft von CultureInfo.DateTimeFormat angegebene Kalender verwendet.

Die CalendarWeekRule-Eigenschaft von CultureInfo.DateTimeFormat enthält den CalendarWeekRule-Standardwert, der eine Kalenderwoche für eine bestimmte Kultur definiert. Hierbei wird der in der Calendar-Eigenschaft von CultureInfo.DateTimeFormat angegebene Kalender verwendet.

Im GregorianCalendar gibt die GetWeekOfYear-Methode beispielsweise für den 1. Januar den Wert 1 zurück.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie das Ergebnis von GetWeekOfYear je nach Verwendung von FirstDayOfWeek und CalendarWeekRule variiert. Wenn es sich bei dem angegebenen Datum um den letzten Tag des Jahres handelt, gibt GetWeekOfYear die Gesamtzahl der Wochen in diesem Jahr an.

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

*/
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).
*/
import System.* ;
import System.Globalization.* ;

public class SamplesCalendar
{
    public static void main(String[] args)
    {
        // Gets the Calendar instance associated with a CultureInfo.
        CultureInfo myCI =  new CultureInfo("en-US");
        Calendar myCal = myCI.get_Calendar();

        // Gets the DTFI properties required by GetWeekOfYear.
        CalendarWeekRule myCWR = 
            myCI.get_DateTimeFormat().get_CalendarWeekRule();
        DayOfWeek myFirstDOW = myCI.get_DateTimeFormat().get_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.", System.Convert.ToString( myCal.GetWeekOfYear(
            DateTime.get_Now(),myCWR, myFirstDOW)));

        // Displays the total number of weeks in the current year.
        DateTime LastDay = 
            new System.DateTime(DateTime.get_Now().get_Year(), 12, 31);
        Console.WriteLine("There are {0} weeks in the current year ({1}).",
            System.Convert.ToString(myCal.GetWeekOfYear(LastDay, myCWR, 
            myFirstDOW)),System.Convert.ToString( LastDay.get_Year()));
    } //main 
} //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 15 of the current year.
There are 53 weeks in the current year (2004).
*/

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Calendar-Klasse
Calendar-Member
System.Globalization-Namespace
System.DateTime
CalendarWeekRule
System.DayOfWeek
GetEra
GetYear
GetMonth
GetDayOfYear
GetDayOfMonth
GetDayOfWeek
GetHour
GetMinute
GetSecond
GetMilliseconds