Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengembalikan minggu dalam setahun yang menyertakan tanggal dalam nilai yang ditentukan DateTime .
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
Parameter
- time
- DateTime
Nilai tanggal dan waktu.
- rule
- CalendarWeekRule
Nilai enumerasi yang menentukan minggu kalender.
- firstDayOfWeek
- DayOfWeek
Nilai enumerasi yang mewakili hari pertama dalam seminggu.
Mengembalikan
Bilangan bulat positif yang mewakili minggu dalam setahun yang menyertakan tanggal dalam time
parameter .
Pengecualian
time
lebih awal dari MinSupportedDateTime atau lebih baru dari MaxSupportedDateTime.
-atau-
firstDayOfWeek
bukan nilai yang valid DayOfWeek .
-atau-
rule
bukan nilai yang valid CalendarWeekRule .
Contoh
Contoh kode berikut menunjukkan bagaimana hasilnya GetWeekOfYear bervariasi tergantung pada FirstDayOfWeek dan yang CalendarWeekRule digunakan. Jika tanggal yang ditentukan adalah hari terakhir dalam setahun, GetWeekOfYear mengembalikan jumlah total minggu dalam tahun tersebut.
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).
Keterangan
Metode ini dapat digunakan untuk menentukan jumlah minggu dalam setahun dengan mengatur time
ke hari terakhir dalam setahun.
Objek DateTimeFormatInfo untuk budaya tertentu yang menggunakan kalender yang ditunjukkan oleh DateTimeFormatInfo.Calendar properti mencakup nilai khusus budaya berikut yang dapat digunakan untuk rule
parameter dan firstDayOfWeek
:
Properti DateTimeFormatInfo.FirstDayOfWeek berisi hari pertama default dalam seminggu yang dapat digunakan untuk
firstDayOfWeek
parameter .Properti DateTimeFormatInfo.CalendarWeekRule berisi aturan minggu kalender default yang dapat digunakan untuk
rule
parameter .
Catatan
Ini tidak memetakan persis ke ISO 8601. Perbedaan dibahas dalam entri blog format ISO 8601 Week of Year di Microsoft .NET. Dimulai dengan .NET Core 3.0, ISOWeek.GetYear dan ISOWeek.GetWeekOfYear selesaikan masalah ini.
Contoh berikut menggunakan objek budaya DateTimeFormatInfo saat ini untuk menentukan bahwa 1 Januari 2011 adalah pada minggu pertama dalam setahun di kalender Gregorian.
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)
Untuk beberapa kalender, panggilan ke metode melempar ArgumentOutOfRangeException untuk kombinasi rule
nilai dan firstDayOfWeek
tertentu meskipun time
lebih besar dari tanggal yang dikembalikan oleh properti kalender tersebutMinSupportedDateTime.GetWeekOfYear Tabel berikut ini mencantumkan kalender yang terpengaruh, nilai tertentu rule
, dan rentang nilai paling awal yang didukung time
. Nilai minimum DateTime tertentu tergantung pada nilai firstDayOfWeek
parameter.
Kalender | Nilai CalendarWeekRule | Tanggal Gregorian (M/dd/yyyy) | Tanggal dalam kalender (M/dd/yyyy) |
---|---|---|---|
ChineseLunisolarCalendar | FirstFullWeek | 19/2/1901 hingga 25/2/1901 | 1/1/1901 hingga 1/7/1901 |
ChineseLunisolarCalendar | FirstFourDayWeek | 19/2/1901 hingga 22/2/1901 | 1/01/1901 hingga 1/04/1901 |
HebrewCalendar | FirstDay | 9/17/1583 | 1/01/5344 |
HebrewCalendar | FirstFullWeek | 17/9/1583 hingga 23/9/1583 | 1/01/5344 hingga 1/07/5344 |
HebrewCalendar | FirstFourDayWeek | 17/9/1583 hingga 20/9/1583 | 1/01/5344 hingga 1/04/5344 |
HijriCalendar | FirstFullWeek | 18/7/0622 hingga 24/7/0622 | 1/01/0001 hingga 1/07/0001 |
HijriCalendar | FirstFourDayWeek | 18/7/0622 hingga 21/7/0622 | 1/01/0001 hingga 1/04/0001 |
JapaneseLunisolarCalendar | FirstFullWeek | 28/1/1960 hingga 2/03/1960 | 1/01/35 hingga 1/07/0035 |
JapaneseLunisolarCalendar | FirstFourDayWeek | 28/1/1960 hingga 31/1/1960 | 1/01/0035 hingga 1/04/0035 |
JulianCalendar | FirstFullWeek | 1/01/0001 hingga 1/05/0001 | 1/03/0001 hingga 1/07/0001 |
JulianCalendar | FirstFourDayWeek | 1/01/0001 hingga 1/02/0001 | 1/03/0001 hingga 1/04/0001 |
KoreanLunisolarCalendar | FirstFullWeek | 14/2/0918 hingga 20/2/0918 | 1/01/0918 hingga 1/07/0918 |
KoreanLunisolarCalendar | FirstFourDayWeek | 14/2/0918 hingga 17/2/0918 | 1/01/0918 hingga 1/04/0918 |
PersianCalendar | FirstFullWeek | 21/3/0622 hingga 27/3/0622 | 1/01/0001 hingga 1/07/0001 |
PersianCalendar | FirstFourDayWeek | 21/3/0622 hingga 24/3/0622 | 1/01/0001 hingga 1/04/0001 |
TaiwanLunisolarCalendar | FirstFullWeek | 18/2/1912 hingga 24/2/1912 | 1/01/0001 hingga 1/07/0001 |
TaiwanLunisolarCalendar | FirstFourDayWeek | 18/2/1912 hingga 21/2/1912 | 1/01/0001 hingga 1/04/0001 |
UmAlQuraCalendar | FirstFullWeek | 30/4/1900 hingga 5/06/1900 | 1/01/1318 hingga 1/07/1318 |
UmAlQuraCalendar | FirstFourDayWeek | 30/4/1900 hingga 5/03/1900 | 1/01/1318 hingga 1/04/1318 |