Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Metoda

Definice

Vrátí týden v roce, který obsahuje datum v zadané DateTime hodnotě.

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

Parametry

time
DateTime

Hodnota data a času

rule
CalendarWeekRule

Hodnota výčtu, která definuje kalendářní týden.

firstDayOfWeek
DayOfWeek

Hodnota výčtu, která představuje první den v týdnu.

Návraty

Kladné celé číslo, které představuje týden v roce, který v parametru time obsahuje datum.

Výjimky

time je dřívější nebo MinSupportedDateTime pozdější než MaxSupportedDateTime.

-nebo-

firstDayOfWeek není platná DayOfWeek hodnota.

-nebo-

rule není platná CalendarWeekRule hodnota.

Příklady

Následující příklad kódu ukazuje, jak se výsledek GetWeekOfYear liší v závislosti na FirstDayOfWeek a použitém CalendarWeekRule . Pokud je zadané datum posledním dnem v roce, GetWeekOfYear vrátí celkový počet týdnů v daném roce.

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

Poznámky

Tuto metodu lze použít k určení počtu týdnů v roce nastavením time na poslední den v roce.

Objekt DateTimeFormatInfo pro konkrétní jazykovou verzi, který používá kalendář označený DateTimeFormatInfo.Calendar vlastností, obsahuje následující hodnoty specifické pro jazykovou verzi, které lze použít pro rule parametry a firstDayOfWeek :

Poznámka

Nemapuje se přesně na ISO 8601. Rozdíly jsou popsány v položce blogu ISO 8601 Týden roku formát v Microsoft .NET. Začněte s .NET Core 3.0 a ISOWeek.GetWeekOfYear tento problém vyřešteISOWeek.GetYear.

Následující příklad používá objekt aktuální jazykové verze DateTimeFormatInfo k určení, že datum 1. ledna 2011 je v prvním týdnu roku v gregoriánském kalendáři.

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)

U některých kalendářů volání metody vyvolá ArgumentOutOfRangeException pro konkrétní kombinace rule hodnot a hodnotu i firstDayOfWeek v případětime, že je větší než datum vrácené vlastností daného kalendářeMinSupportedDateTime.GetWeekOfYear Následující tabulka uvádí ovlivněné kalendáře, konkrétní rule hodnoty a rozsah nejstarších podporovaných time hodnot. Konkrétní minimální DateTime hodnota závisí na hodnotě parametru firstDayOfWeek .

Kalendář Hodnota CalendarWeekRule Gregoriánské datum (M/dd/yyyy) Datum v kalendáři (M/dd/yyyy)
ChineseLunisolarCalendar FirstFullWeek 2/19/1901 až 25/2/1901 1.1.1901 až 1.7.1901
ChineseLunisolarCalendar FirstFourDayWeek 2/19/1901 až 22/1901 1/01/1901 až 1/04/1901
HebrewCalendar FirstDay 9/17/1583 1/01/5344
HebrewCalendar FirstFullWeek 17.9.1583 až 23.9.1583 1/01/5344 až 1/07/5344
HebrewCalendar FirstFourDayWeek 17/9/1583 až 20/1583 1/01/5344 až 1/04/5344
HijriCalendar FirstFullWeek 7/18/0622 až 7/24/0622 1/01/0001 až 1/07/0001
HijriCalendar FirstFourDayWeek 18.7.0622 až 21.7.0622 1/01/0001 až 1/04/0001
JapaneseLunisolarCalendar FirstFullWeek 28.1.1960 až 2.3.1960 1/01/35 až 1/07/0035
JapaneseLunisolarCalendar FirstFourDayWeek 28.1.1960 až 31.1.1960 1/01/0035 až 1/04/0035
JulianCalendar FirstFullWeek 1/01/0001 až 1/05/0001 1/03/0001 až 1/07/0001
JulianCalendar FirstFourDayWeek 1/01/0001 až 1/02/0001 1/03/0001 až 1/04/0001
KoreanLunisolarCalendar FirstFullWeek 2/14/0918 až 20/0918 1/01/0918 až 1/07/0918
KoreanLunisolarCalendar FirstFourDayWeek 2/14/0918 až 2/17/0918 1/01/0918 až 1/04/0918
PersianCalendar FirstFullWeek 3/21/0622 až 27/0622 1/01/0001 až 1/07/0001
PersianCalendar FirstFourDayWeek 3/21/0622 až 3/24/0622 1/01/0001 až 1/04/0001
TaiwanLunisolarCalendar FirstFullWeek 2/18/1912 až 24/1912 1/01/0001 až 1/07/0001
TaiwanLunisolarCalendar FirstFourDayWeek 2/18/1912 až 21/2/1912 1/01/0001 až 1/04/0001
UmAlQuraCalendar FirstFullWeek 4/30/1900 až 5/06/1900 1/01/1318 až 1/07/1318
UmAlQuraCalendar FirstFourDayWeek 30.4.1900 až 5.3.1900 1/01/1318 až 1/04/1318

Platí pro

Viz také