Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) 方法
定义
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
参数
- time
- DateTime
日期和时间值。A date and time value.
- rule
- CalendarWeekRule
定义日历周的枚举值。An enumeration value that defines a calendar week.
- firstDayOfWeek
- DayOfWeek
表示一周的第一天的枚举值。An enumeration value that represents the first day of the week.
返回
一个正整数,用于表示一年中包含 time 参数内日期所在的星期。A positive integer that represents the week of the year that includes the date in the time parameter.
例外
time 早于 MinSupportedDateTime 或晚于 MaxSupportedDateTime。time is earlier than MinSupportedDateTime or later than MaxSupportedDateTime.
- 或 --or-
firstDayOfWeek 不是有效的 DayOfWeek 值。firstDayOfWeek is not a valid DayOfWeek value.
- 或 --or-
rule 不是有效的 CalendarWeekRule 值。rule is not a valid CalendarWeekRule value.
示例
下面的代码示例演示如何根据 GetWeekOfYear 所使用的和的结果 FirstDayOfWeek CalendarWeekRule 。The following code example shows how the result of GetWeekOfYear varies depending on the FirstDayOfWeek and the CalendarWeekRule used. 如果指定的日期是一年中的最后一天,则 GetWeekOfYear 返回该年份的总周数。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).
*/
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).
注解
此方法可用于通过将设置为一年中的 time 最后一天来确定该年份中的周数。This method can be used to determine the number of weeks in the year by setting time to the last day of the year.
DateTimeFormatInfo使用属性指示的日历的特定区域性的对象 DateTimeFormatInfo.Calendar 包括以下可用于和参数的区域性特定值 rule firstDayOfWeek :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:
DateTimeFormatInfo.FirstDayOfWeek属性包含可用于参数的第一周的第一天
firstDayOfWeek。The DateTimeFormatInfo.FirstDayOfWeek property contains the default first day of the week that can be used for thefirstDayOfWeekparameter.DateTimeFormatInfo.CalendarWeekRule属性包含可用于参数的默认日历周规则
rule。The DateTimeFormatInfo.CalendarWeekRule property contains the default calendar week rule that can be used for theruleparameter.
备注
这不会完全映射到 ISO 8601。This does not map exactly to ISO 8601. Microsoft .NET 中,博客条目中的每 年 ISO 8601 周格式讨论了这些差异。The differences are discussed in the blog entry ISO 8601 Week of Year format in Microsoft .NET. 从 .NET Core 3.0 开始, ISOWeek.GetYear 并 ISOWeek.GetWeekOfYear 解决此问题。Starting with .NET Core 3.0, ISOWeek.GetYear and ISOWeek.GetWeekOfYear solve this problem.
下面的示例使用当前区域性的 DateTimeFormatInfo 对象确定2011在公历的第一周的第一周。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.
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)
对于某些日历,对方法的调用对 GetWeekOfYear ArgumentOutOfRangeException 和值的特定组合引发 rule , firstDayOfWeek 即使 time 大于该日历的属性返回的日期也是如此 MinSupportedDateTime 。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. 下表列出了受影响的日历、特定 rule 值以及支持的最早值的范围 time 。The following table lists the affected calendars, the specific rule values, and the range of the earliest supported time values. 具体的最小 DateTime 值取决于参数的值 firstDayOfWeek 。The specific minimum DateTime value depends on the value of the firstDayOfWeek parameter.
| 日历Calendar | CalendarWeekRule 值CalendarWeekRule value | 公历日期 (M/dd/yyyy) Gregorian date (M/dd/yyyy) | 日历中的日期 (M/dd/yyyy) Date in calendar (M/dd/yyyy) |
|---|---|---|---|
| ChineseLunisolarCalendar | FirstFullWeek | 2/19/1901 至2/25/19012/19/1901 to 2/25/1901 | 1/1/1901 至1/7/19011/1/1901 to 1/7/1901 |
| ChineseLunisolarCalendar | FirstFourDayWeek | 2/19/1901 至2/22/19012/19/1901 to 2/22/1901 | 1/01/1901 至1/04/19011/01/1901 to 1/04/1901 |
| HebrewCalendar | FirstDay | 9/17/15839/17/1583 | 1/01/53441/01/5344 |
| HebrewCalendar | FirstFullWeek | 9/17/1583 至9/23/15839/17/1583 to 9/23/1583 | 1/01/5344 至1/07/53441/01/5344 to 1/07/5344 |
| HebrewCalendar | FirstFourDayWeek | 9/17/1583 至9/20/15839/17/1583 to 9/20/1583 | 1/01/5344 至1/04/53441/01/5344 to 1/04/5344 |
| HijriCalendar | FirstFullWeek | 7/18/0622 至7/24/06227/18/0622 to 7/24/0622 | 1/01/0001 至1/07/00011/01/0001 to 1/07/0001 |
| HijriCalendar | FirstFourDayWeek | 7/18/0622 至7/21/06227/18/0622 to 7/21/0622 | 1/01/0001 至1/04/00011/01/0001 to 1/04/0001 |
| JapaneseLunisolarCalendar | FirstFullWeek | 1/28/1960 至2/03/19601/28/1960 to 2/03/1960 | 1/01/35 至1/07/00351/01/35 to 1/07/0035 |
| JapaneseLunisolarCalendar | FirstFourDayWeek | 1/28/1960 至1/31/19601/28/1960 to 1/31/1960 | 1/01/0035 至1/04/00351/01/0035 to 1/04/0035 |
| JulianCalendar | FirstFullWeek | 1/01/0001 至1/05/00011/01/0001 to 1/05/0001 | 1/03/0001 至1/07/00011/03/0001 to 1/07/0001 |
| JulianCalendar | FirstFourDayWeek | 1/01/0001 至1/02/00011/01/0001 to 1/02/0001 | 1/03/0001 至1/04/00011/03/0001 to 1/04/0001 |
| KoreanLunisolarCalendar | FirstFullWeek | 2/14/0918 至2/20/09182/14/0918 to 2/20/0918 | 1/01/0918 至1/07/09181/01/0918 to 1/07/0918 |
| KoreanLunisolarCalendar | FirstFourDayWeek | 2/14/0918 至2/17/09182/14/0918 to 2/17/0918 | 1/01/0918 至1/04/09181/01/0918 to 1/04/0918 |
| PersianCalendar | FirstFullWeek | 3/21/0622 至3/27/06223/21/0622 to 3/27/0622 | 1/01/0001 至1/07/00011/01/0001 to 1/07/0001 |
| PersianCalendar | FirstFourDayWeek | 3/21/0622 至3/24/06223/21/0622 to 3/24/0622 | 1/01/0001 至1/04/00011/01/0001 to 1/04/0001 |
| TaiwanLunisolarCalendar | FirstFullWeek | 2/18/1912 至2/24/19122/18/1912 to 2/24/1912 | 1/01/0001 至1/07/00011/01/0001 to 1/07/0001 |
| TaiwanLunisolarCalendar | FirstFourDayWeek | 2/18/1912 至2/21/19122/18/1912 to 2/21/1912 | 1/01/0001 至1/04/00011/01/0001 to 1/04/0001 |
| UmAlQuraCalendar | FirstFullWeek | 4/30/1900 至5/06/19004/30/1900 to 5/06/1900 | 1/01/1318 至1/07/13181/01/1318 to 1/07/1318 |
| UmAlQuraCalendar | FirstFourDayWeek | 4/30/1900 至5/03/19004/30/1900 to 5/03/1900 | 1/01/1318 至1/04/13181/01/1318 to 1/04/1318 |