Calendar::GetWeekOfYear Method (DateTime, CalendarWeekRule, DayOfWeek)
Returns the week of the year that includes the date in the specified DateTime value.
Assembly: mscorlib (in mscorlib.dll)
public: virtual int GetWeekOfYear( DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek )
Parameters
- time
-
Type:
System::DateTime
A date and time value.
- rule
-
Type:
System.Globalization::CalendarWeekRule
An enumeration value that defines a calendar week.
- firstDayOfWeek
-
Type:
System::DayOfWeek
An enumeration value that represents the first day of the week.
Return Value
Type: System::Int32A positive integer that represents the week of the year that includes the date in the time parameter.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | time is earlier than MinSupportedDateTime or later than MaxSupportedDateTime. -or- firstDayOfWeek is not a valid DayOfWeek value. -or- rule is not a valid CalendarWeekRule value. |
This method can be used to determine the number of weeks in the year by setting time to the last day of the year.
The DateTimeFormatInfo object for a particular culture that uses the calendar indicated by the DateTimeFormatInfo::Calendar property includes the following culture-specific values that can be used for the rule and firstDayOfWeek parameters:
The DateTimeFormatInfo::FirstDayOfWeek property contains the default first day of the week that can be used for the firstDayOfWeek parameter.
The DateTimeFormatInfo::CalendarWeekRule property contains the default calendar week rule that can be used for the rule parameter.
The following example uses the current culture's DateTimeFormatInfo object to determine that January 1, 2011 is in the first week of the year in the Gregorian calendar.
For some calendars, a call to the GetWeekOfYear method throws an ArgumentOutOfRangeException for particular combinations of rule and firstDayOfWeek values even if time is greater than the date returned by that calendar's MinSupportedDateTime property. The following table lists the affected calendars, the specific rule values, and the range of the earliest supported time values. The specific minimum DateTime value depends on the value of the firstDayOfWeek parameter.
Calendar | CalendarWeekRule value | Gregorian date (M/dd/yyyy) | Date in calendar (M/dd/yyyy) |
|---|---|---|---|
2/19/1901 to 2/25/1901 | 1/1/1901 to 1/7/1901 | ||
2/19/1901 to 2/22/1901 | 1/01/1901 to 1/04/1901 | ||
9/17/1583 | 1/01/5344 | ||
9/17/1583 to 9/23/1583 | 1/01/5344 to 1/07/5344 | ||
9/17/1583 to 9/20/1583 | 1/01/5344 to 1/04/5344 | ||
7/18/0622 to 7/24/0622 | 1/01/0001 to 1/07/0001 | ||
7/18/0622 to 7/21/0622 | 1/01/0001 to 1/04/0001 | ||
1/28/1960 to 2/03/1960 | 1/01/35 to 1/07/0035 | ||
1/28/1960 to 1/31/1960 | 1/01/0035 to 1/04/0035 | ||
1/01/0001 to 1/05/0001 | 1/03/0001 to 1/07/0001 | ||
1/01/0001 to 1/02/0001 | 1/03/0001 to 1/04/0001 | ||
2/14/0918 to 2/20/0918 | 1/01/0918 to 1/07/0918 | ||
2/14/0918 to 2/17/0918 | 1/01/0918 to 1/04/0918 | ||
3/21/0622 to 3/27/0622 | 1/01/0001 to 1/07/0001 | ||
3/21/0622 to 3/24/0622 | 1/01/0001 to 1/04/0001 | ||
2/18/1912 to 2/24/1912 | 1/01/0001 to 1/07/0001 | ||
2/18/1912 to 2/21/1912 | 1/01/0001 to 1/04/0001 | ||
4/30/1900 to 5/06/1900 | 1/01/1318 to 1/07/1318 | ||
4/30/1900 to 5/03/1900 | 1/01/1318 to 1/04/1318 |
The following code example shows how the result of GetWeekOfYear varies depending on the FirstDayOfWeek and the CalendarWeekRule used. If the specified date is the last day of the year, GetWeekOfYear returns the total number of weeks in that year.
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). */
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1