Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce la settimana dell'anno che comprende la data nel valore DateTime specificato.
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
Parametri
- time
- DateTime
Valore di data e ora.
- rule
- CalendarWeekRule
Valore di enumerazione che definisce una settimana di calendario.
- firstDayOfWeek
- DayOfWeek
Valore di enumerazione che rappresenta il primo giorno della settimana.
Restituisce
Valore intero positivo che rappresenta la settimana dell'anno che include la data nel parametro time
.
Eccezioni
time
è precedente a MinSupportedDateTime o successivo a MaxSupportedDateTime.
-oppure-
firstDayOfWeek
non è un valore valido di DayOfWeek.
-oppure-
rule
non è un valore valido di CalendarWeekRule.
Esempio
Nell'esempio di codice seguente viene illustrato il modo in cui il risultato varia a seconda dell'oggetto GetWeekOfYear e dell'oggetto FirstDayOfWeekCalendarWeekRule usato. Se la data specificata è l'ultimo giorno dell'anno, GetWeekOfYear restituisce il numero totale di settimane in tale anno.
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).
Commenti
Questo metodo può essere usato per determinare il numero di settimane nell'anno impostando time
sull'ultimo giorno dell'anno.
L'oggetto DateTimeFormatInfo per una determinata cultura che usa il calendario indicato dalla DateTimeFormatInfo.Calendar proprietà include i valori specifici delle impostazioni cultura seguenti che possono essere usati per i rule
parametri e firstDayOfWeek
:
La DateTimeFormatInfo.FirstDayOfWeek proprietà contiene il primo giorno predefinito della settimana che può essere usato per il
firstDayOfWeek
parametro.La DateTimeFormatInfo.CalendarWeekRule proprietà contiene la regola della settimana del calendario predefinita che può essere usata per il
rule
parametro.
Nota
Questo non esegue il mapping esattamente a ISO 8601. Le differenze sono descritte nella voce di blog ISO 8601 Week of Year in Microsoft .NET. A partire da .NET Core 3.0 ISOWeek.GetYear e ISOWeek.GetWeekOfYear risolvere questo problema.
Nell'esempio seguente viene usato l'oggetto cultura corrente per determinare che il 1° gennaio 2011 si trova nella prima settimana dell'anno DateTimeFormatInfo nel calendario gregoriano.
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)
Per alcuni calendari, una chiamata al GetWeekOfYear metodo genera una ArgumentOutOfRangeException combinazione specifica di rule
valori e firstDayOfWeek
anche se time
è maggiore della data restituita dalla proprietà del MinSupportedDateTime calendario. Nella tabella seguente sono elencati i calendari interessati, i valori specifici rule
e l'intervallo dei valori più antichi supportati time
. Il valore minimo DateTime specifico dipende dal valore del firstDayOfWeek
parametro.
Calendario | Valore CalendarWeekRule | Data gregoriana (M/dd/yyyy) | Data nel calendario (M/dd/yyyy) |
---|---|---|---|
ChineseLunisolarCalendar | FirstFullWeek | Da 2/19/1901 a 2/25/1901 | Da 1/1/1901 a 1/7/1901 |
ChineseLunisolarCalendar | FirstFourDayWeek | Da 2/19/1901 a 2/22/1901 | Da 1/01/1901 a 1/04/1901 |
HebrewCalendar | FirstDay | 9/17/1583 | 1/01/5344 |
HebrewCalendar | FirstFullWeek | Da 9/17/1583 a 9/23/1583 | 1/01/5344 a 1/07/53444 |
HebrewCalendar | FirstFourDayWeek | Da 9/17/1583 a 9/20/1583 | 1/01/53444 a 1/04/53444 |
HijriCalendar | FirstFullWeek | Da 7/18/0622 a 7/24/0622 | Da 1/01/0001 a 1/07/0001 |
HijriCalendar | FirstFourDayWeek | Da 7/18/0622 a 7/21/0622 | Da 1/01/0001 a 1/04/0001 |
JapaneseLunisolarCalendar | FirstFullWeek | Da 1/28/1960 a 2/03/1960 | Da 1/01/35 a 1/07/0035 |
JapaneseLunisolarCalendar | FirstFourDayWeek | Da 1/28/1960 a 1/31/1960 | 1/01/0035 a 1/04/0035 |
JulianCalendar | FirstFullWeek | Da 1/01/0001 a 1/05/0001 | Da 1/03/0001 a 1/07/0001 |
JulianCalendar | FirstFourDayWeek | Da 1/01/0001 a 1/02/0001 | Da 1/03/0001 a 1/04/0001 |
KoreanLunisolarCalendar | FirstFullWeek | Da 2/14/0918 a 2/20/0918 | 1/01/0918 a 1/07/0918 |
KoreanLunisolarCalendar | FirstFourDayWeek | Da 2/14/0918 a 2/17/0918 | Da 1/01/0918 a 1/04/0918 |
PersianCalendar | FirstFullWeek | Da 3/21/0622 a 3/27/0622 | Da 1/01/0001 a 1/07/0001 |
PersianCalendar | FirstFourDayWeek | Da 3/21/0622 a 3/24/0622 | Da 1/01/0001 a 1/04/0001 |
TaiwanLunisolarCalendar | FirstFullWeek | Da 2/18/1912 a 2/24/1912 | Da 1/01/0001 a 1/07/0001 |
TaiwanLunisolarCalendar | FirstFourDayWeek | Da 2/18/1912 a 2/21/1912 | Da 1/01/0001 a 1/04/0001 |
UmAlQuraCalendar | FirstFullWeek | Da 4/30/1900 a 5/06/1900 | Da 1/01/1318 a 1/07/1318 |
UmAlQuraCalendar | FirstFourDayWeek | Da 4/30/1900 a 5/03/1900 | Da 1/01/1318 a 1/04/1318 |