CalendarWeekRule CalendarWeekRule CalendarWeekRule CalendarWeekRule Enum

定义

定义确定年份第一周的不同规则。Defines different rules for determining the first week of the year.

public enum class CalendarWeekRule
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum CalendarWeekRule
type CalendarWeekRule = 
Public Enum CalendarWeekRule
继承
CalendarWeekRuleCalendarWeekRuleCalendarWeekRuleCalendarWeekRule
属性

字段

FirstDay FirstDay FirstDay FirstDay 0

指示年的第一周从该年的第一天开始,到所指定周的下一个首日前结束。Indicates that the first week of the year starts on the first day of the year and ends before the following designated first day of the week. 值为 0。The value is 0.

FirstFourDayWeek FirstFourDayWeek FirstFourDayWeek FirstFourDayWeek 2

指示年的第一周是第一个在指定的周首日前包含四天或更多天的周。Indicates that the first week of the year is the first week with four or more days before the designated first day of the week. 值为 2。The value is 2.

FirstFullWeek FirstFullWeek FirstFullWeek FirstFullWeek 1

指示年的第一周从所指定周首日的第一个匹配项开始,可以是年的第一天或其后某一天。Indicates that the first week of the year begins on the first occurrence of the designated first day of the week on or after the first day of the year. 值为 1。The value is 1.

注解

CalendarWeekRule 属性返回并由区域性的当前日历用来确定日历周规则的 DateTimeFormatInfo.CalendarWeekRule 枚举的成员。A member of the CalendarWeekRule enumeration is returned by the DateTimeFormatInfo.CalendarWeekRule property and is used by the culture's current calendar to determine the calendar week rule. 枚举值还用作 Calendar.GetWeekOfYear 方法的参数。The enumeration value is also used as a parameter to the Calendar.GetWeekOfYear method.

日历周规则取决于表示一周的第一天的 System.DayOfWeek 值以及 CalendarWeekRule 值。Calendar week rules depend on the System.DayOfWeek value that indicates the first day of the week in addition to depending on a CalendarWeekRule value. 属性为区域性提供默认值, 但可以在Calendar.GetWeekOfYear方法中DayOfWeek将任何值指定为一周的第一天。 DateTimeFormatInfo.FirstDayOfWeekThe DateTimeFormatInfo.FirstDayOfWeek property provides the default value for a culture, but any DayOfWeek value can be specified as the first day of the week in the Calendar.GetWeekOfYear method.

基于FirstDay该值的第一周可以有一到七天。The first week based on the FirstDay value can have one to seven days. 基于FirstFullWeek该值的第一周始终有七天。The first week based on the FirstFullWeek value always has seven days. 基于FirstFourDayWeek该值的第一周可以有四到七天。The first week based on the FirstFourDayWeek value can have four to seven days.

例如, 在公历中, 假设一年的第一天 (1 月1日) 落在星期二, 指定的一周的第一天是星期日。For example, in the Gregorian calendar, suppose that the first day of the year (January 1) falls on a Tuesday and the designated first day of the week is Sunday. 选择FirstFullWeek将第一个星期日 (1 月6日) 定义为该年第一周的开头。Selecting FirstFullWeek defines the first Sunday (January 6) as the beginning of the first week of the year. 每年的前五天被视为上一年最后一周的一部分。The first five days of the year are considered part of the last week of the previous year. 相反, 选择FirstFourDayWeek将定义一年中的第一天 (1 月1日) 作为该年第一周的开始, 因为从1月1日到下星期日之前的一天有超过四天的时间。In contrast, selecting FirstFourDayWeek defines the first day of the year (January 1) as the beginning of the first week of the year because there are more than four days from January 1 to the day before the following Sunday.

日期Date FirstDayFirstDay FirstFullWeekFirstFullWeek FirstFourDayWeekFirstFourDayWeek
12月31日Dec 31 Mon 上一年度的最后一周Last week of the previous year 上一年度的最后一周Last week of the previous year 上一年度的最后一周Last week of the previous year
1月1日周二Jan 1 Tue 第1周Week 1 上一年度的最后一周Last week of the previous year 第1周Week 1
Jan 2 周三Jan 2 Wed 第1周Week 1 上一年度的最后一周Last week of the previous year 第1周Week 1
Jan 3 ThuJan 3 Thu 第1周Week 1 上一年度的最后一周Last week of the previous year 第1周Week 1
1月4日星期五Jan 4 Fri 第1周Week 1 上一年度的最后一周Last week of the previous year 第1周Week 1
1月5日Jan 5 Sat 第1周Week 1 上一年度的最后一周Last week of the previous year 第1周Week 1
1月6日Jan 6 Sun 第2周Week 2 第1周Week 1 第2周Week 2
Jan 7 周一Jan 7 Mon 第2周Week 2 第1周Week 1 第2周Week 2
Jan 8 周二Jan 8 Tue 第2周Week 2 第1周Week 1 第2周Week 2
Jan 9 周三Jan 9 Wed 第2周Week 2 第1周Week 1 第2周Week 2
Jan 10 ThuJan 10 Thu 第2周Week 2 第1周Week 1 第2周Week 2
1月11日星期五Jan 11 Fri 第2周Week 2 第1周Week 1 第2周Week 2
1月12日Jan 12 Sat 第2周Week 2 第1周Week 1 第2周Week 2

假设年的第一天 (1 月1日) 在星期五, 指定的一周的第一天是星期日。Suppose the first day of the year (January 1) falls on a Friday and the designated first day of the week is Sunday. 选择FirstFourDayWeek将第一个星期日 (1 月3日) 定义为该年第一周的第一周的开始, 因为从1月1日到下一个星期日之前的一天的时间少于四天。Selecting FirstFourDayWeek defines the first Sunday (January 3) as the beginning of the first week of the year because there are fewer than four days from January 1 to the day before the following Sunday.

日期Date FirstDayFirstDay FirstFullWeekFirstFullWeek FirstFourDayWeekFirstFourDayWeek
12月31日 ThuDec 31 Thu 上一年度的最后一周Last week of the previous year 上一年度的最后一周Last week of the previous year 上一年度的最后一周Last week of the previous year
1月1日星期五Jan 1 Fri 第1周Week 1 上一年度的最后一周Last week of the previous year 上一年度的最后一周Last week of the previous year
1月2日Jan 2 Sat 第1周Week 1 上一年度的最后一周Last week of the previous year 上一年度的最后一周Last week of the previous year
1月3日Jan 3 Sun 第2周Week 2 第1周Week 1 第1周Week 1
1月4日Jan 4 Mon 第2周Week 2 第1周Week 1 第1周Week 1
Jan 5 周二Jan 5 Tue 第2周Week 2 第1周Week 1 第1周Week 1
Jan 6 周三Jan 6 Wed 第2周Week 2 第1周Week 1 第1周Week 1
Jan 7 ThuJan 7 Thu 第2周Week 2 第1周Week 1 第1周Week 1
1月8日星期五Jan 8 Fri 第2周Week 2 第1周Week 1 第1周Week 1
Jan 9 周六Jan 9 Sat 第2周Week 2 第1周Week 1 第1周Week 1

下面的示例阐释如何将 CalendarWeekRuleDayOfWeek 值结合使用来确定分配周的方式。The following example illustrates how the CalendarWeekRule and DayOfWeek values are used together to determine how weeks are assigned. 在公历日历中,2013 年的第一天(1 月 1 日)是星期二。In the Gregorian calendar, the first day of the year (January 1) in 2013 falls on a Tuesday. 如果指定的一周中的第一天是星期日,则第一个星期日(1 月 6 日)是该年第一周的第一天,并且星期六(1 月 5 日)属于上一年的第 53 周。If the designated first day of the week is Sunday, the first Sunday (January 6) is the first day of the first week of the year, and Saturday (January 5) belongs to the fifty-third week of the previous year. 将日历周规则更改为 FirstFourDayWeek 会将星期二(1 月 1 日)定义为该年第一周的开始,因为星期二(1 月 1 日)和星期日(1 月 6 日)之间的天数超过了 4 天。Changing the calendar week rule to FirstFourDayWeek defines Tuesday (January 1) as the beginning of the first week of the year, because there are more than four days between Tuesday, January 1, and Sunday, January 6. 通过使用此规则,1 月 5 日将属于该年的第一周。Using this rule, January 5 belongs to the first week of the year. 对于 2010 年,该年的 1 月 1 日为星期五,使用 FirstFourDayWeek 规则,并且 DayOfWeek.Sunday 为周中的第一天,星期日(1 月 3 日)为该年第一周的开始,因为 2010 年的第一周为 1 月 3 日到 1 月 9 日,超过了 4 天。For 2010, a year in which January 1 falls on a Friday, applying the FirstFourDayWeek rule with DayOfWeek.Sunday as the first day of the week makes Sunday, January 3 the beginning of the first week of the year, because the first week in 2010 that has more than four days is January 3 through 9.

using System;
using System.Globalization;

public class Example
{
   static Calendar cal = new GregorianCalendar();
   
   public static void Main()
   {
      DateTime date = new DateTime(2013, 1, 5);
      DayOfWeek firstDay = DayOfWeek.Sunday;
      CalendarWeekRule rule;
      
      rule = CalendarWeekRule.FirstFullWeek;
      ShowWeekNumber(date, rule, firstDay);
      
      rule = CalendarWeekRule.FirstFourDayWeek;
      ShowWeekNumber(date, rule, firstDay);
      
      Console.WriteLine();
      date = new DateTime(2010, 1, 3);
      ShowWeekNumber(date, rule, firstDay);
   }

   private static void ShowWeekNumber(DateTime dat, CalendarWeekRule rule, 
                                      DayOfWeek firstDay)
   {                                   
      Console.WriteLine("{0:d} with {1:F} rule and {2:F} as first day of week: week {3}",
                        dat, rule, firstDay, cal.GetWeekOfYear(dat, rule, firstDay));
   }   
}
// The example displays the following output:
//       1/5/2013 with FirstFullWeek rule and Sunday as first day of week: week 53
//       1/5/2013 with FirstFourDayWeek rule and Sunday as first day of week: week 1
//       
//       1/3/2010 with FirstFourDayWeek rule and Sunday as first day of week: week 1
Imports System.Globalization

Module Example
   
   Dim cal As New GregorianCalendar()
   
   Public Sub Main()
      Dim dat As Date = #01/05/2013#
      Dim firstDay As DayOfWeek = DayOfWeek.Sunday
      Dim rule As CalendarWeekRule
      
      rule = CalendarWeekRule.FirstFullWeek
      ShowWeekNumber(dat, rule, firstDay)
      
      rule = CalendarWeekRule.FirstFourDayWeek
      ShowWeekNumber(dat, rule, firstDay)

      Console.WriteLine()
      dat = #1/03/2010#
      ShowWeekNumber(dat, rule, firstDay)
   End Sub
   
   Private Sub ShowWeekNumber(dat As Date, rule As CalendarWeekRule, 
                              firstDay As DayOfWeek)
      Console.WriteLine("{0:d} with {1:F} rule and {2:F} as first day of week: week {3}",
                        dat, rule, firstDay, cal.GetWeekOfYear(dat, rule, firstDay))
   End Sub   
End Module
' The example displays the following output:
'       1/5/2013 with FirstFullWeek rule and Sunday as first day of week: week 53
'       1/5/2013 with FirstFourDayWeek rule and Sunday as first day of week: week 1
'       
'       1/3/2010 with FirstFourDayWeek rule and Sunday as first day of week: week 1

备注

这不会完全映射到 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.GetYearISOWeek.GetWeekOfYear解决此问题。Starting with .NET Core 3.0, ISOWeek.GetYear and ISOWeek.GetWeekOfYear solve this problem.

CultureInfo个对象都支持一组日历。Each CultureInfo object supports a set of calendars. 属性返回区域性的默认日历, OptionalCalendars属性返回一个数组, 该数组包含该区域性支持的所有日历。 CalendarThe Calendar property returns the default calendar for the culture, and the OptionalCalendars property returns an array containing all the calendars supported by the culture. 若要更改CultureInfo使用的日历, 请Calendar将的属性CultureInfo.DateTimeFormat设置为新Calendar的。To change the calendar used by a CultureInfo, set the Calendar property of CultureInfo.DateTimeFormat to a new Calendar.

适用于

另请参阅