Aracılığıyla paylaş


Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Yöntem

Tanım

Belirtilen DateTime değerdeki tarihi içeren yılın haftasını döndürür.

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

Parametreler

time
DateTime

Tarih ve saat değeri.

rule
CalendarWeekRule

Takvim haftası tanımlayan bir numaralandırma değeri.

firstDayOfWeek
DayOfWeek

Haftanın ilk gününü temsil eden bir numaralandırma değeri.

Döndürülenler

parametresinde tarihi time içeren yılın haftasını temsil eden pozitif bir tamsayı.

Özel durumlar

time , değerinden önceki MinSupportedDateTime veya sonraki bir sürümdür MaxSupportedDateTime.

-veya-

firstDayOfWeek geçerli DayOfWeek bir değer değil.

-veya-

rule geçerli CalendarWeekRule bir değer değil.

Örnekler

Aşağıdaki kod örneği, sonucunun GetWeekOfYear kullanılan ve CalendarWeekRule öğesine bağlı FirstDayOfWeek olarak nasıl değiştiğini gösterir. Belirtilen tarih yılın son günüyse, GetWeekOfYear o yıldaki toplam hafta sayısını döndürür.

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

Açıklamalar

Bu yöntem, yılın son gününe ayarlanarak time yıl içindeki hafta sayısını belirlemek için kullanılabilir.

DateTimeFormatInfo özelliği tarafından belirtilen takvimi kullanan belirli bir kültürün DateTimeFormatInfo.Calendar nesnesi, ve firstDayOfWeek parametreleri için rule kullanılabilecek kültüre özgü aşağıdaki değerleri içerir:

Not

Bu, ISO 8601 ile tam olarak eşlenmez. Farklılıklar , Microsoft .NET'teki ISO 8601 Yılın Haftası biçiminde blog girdisinde ele alınmaktadır. .NET Core 3.0'dan ISOWeek.GetYearISOWeek.GetWeekOfYear başlayarak bu sorunu çözün.

Aşağıdaki örnek, 1 Ocak 2011'in DateTimeFormatInfo Gregoryen takvimde yılın ilk haftasında olduğunu belirlemek için geçerli kültürün nesnesini kullanır.

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)

Bazı takvimler için yöntemine yapılan GetWeekOfYear bir çağrı, takvimin firstDayOfWeekruleMinSupportedDateTime özelliği tarafından döndürülen tarihten büyük olsa time bile ve değerlerinin belirli birleşimleri için bir ArgumentOutOfRangeException oluşturur. Aşağıdaki tabloda etkilenen takvimler, belirli rule değerler ve desteklenen time en erken değerlerin aralığı listelenmektedir. Belirli bir minimum DateTime değer parametresinin firstDayOfWeek değerine bağlıdır.

Takvim CalendarWeekRule değeri Gregoryen tarih (M/dd/yyyy) Takvimdeki tarih (M/dd/yyyy)
ChineseLunisolarCalendar FirstFullWeek 19.02.1901 - 25.02.1901 1/1/1901 - 7/1/1901
ChineseLunisolarCalendar FirstFourDayWeek 19.02.1901 - 22.02.1901 01.01.1901 - 04.01.1901
HebrewCalendar FirstDay 9/17/1583 1/01/5344
HebrewCalendar FirstFullWeek 17/9/1583 - 23/9/1583 01.01.5344 - 07.01.5344
HebrewCalendar FirstFourDayWeek 17/9/1583 - 20/9/1583 01.01.5344 - 04.01.5344
HijriCalendar FirstFullWeek 18.07.0622 - 24.07.0622 01.01.0001 - 07.01.0001
HijriCalendar FirstFourDayWeek 18.07.0622 - 21.07.0622 01.01.0001 - 04.01.0001
JapaneseLunisolarCalendar FirstFullWeek 28.1.1960 - 03.02.1960 01.01.35 - 07.01.0035
JapaneseLunisolarCalendar FirstFourDayWeek 28.1.1960 - 31.01.1960 01.01.0035 - 04.01.0035
JulianCalendar FirstFullWeek 01.01.0001 - 05.01.0001 03.03.0001 - 07.01.0001
JulianCalendar FirstFourDayWeek 01.01.0001 - 02.01.0001 03.03.0001 - 04.01.0001
KoreanLunisolarCalendar FirstFullWeek 14.02.0918 - 20.02.0918 01.01.0918 - 07.01.0918
KoreanLunisolarCalendar FirstFourDayWeek 14.02.0918 - 17.02.0918 01.01.0918 - 04.01.0918
PersianCalendar FirstFullWeek 21.06.0622 - 27.03.0622 01.01.0001 - 07.01.0001
PersianCalendar FirstFourDayWeek 21.06.0622 - 24.03.0622 01.01.0001 - 04.01.0001
TaiwanLunisolarCalendar FirstFullWeek 18/2/1912 - 24/2/1912 01.01.0001 - 07.01.0001
TaiwanLunisolarCalendar FirstFourDayWeek 18.02.1912 - 21.02.1912 01.01.0001 - 04.01.0001
UmAlQuraCalendar FirstFullWeek 30.04.1900 - 06.05.1900 01.01.1318 - 07.01.1318
UmAlQuraCalendar FirstFourDayWeek 30.04.1900 - 03.05.1900 01.01.1318 - 04.01.1318

Şunlara uygulanır

Ayrıca bkz.