CalendarWeekRule 列舉

定義

定義決定年份第一週的各種規則。Defines different rules for determining the first week of the year.

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

欄位

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 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 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.

備註

DateTimeFormatInfo.CalendarWeekRule 屬性會傳回 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. [DateTimeFormatInfo.FirstDayOfWeek] 屬性會提供文化特性的預設值,但任何 DayOfWeek 值都可以指定為 Calendar.GetWeekOfYear 方法中一周的第一天。The 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
Jan 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
Jan 4 星期五Jan 4 Fri 第1周Week 1 上一年的最後一周Last week of the previous year 第1周Week 1
Jan 5 SatJan 5 Sat 第1周Week 1 上一年的最後一周Last week of the previous year 第1周Week 1
Jan 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
Jan 11 星期五Jan 11 Fri 第2周Week 2 第1周Week 1 第2周Week 2
Jan 12 SatJan 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日星期四Dec 31 Thu 上一年的最後一周Last week of the previous year 上一年的最後一周Last week of the previous year 上一年的最後一周Last week of the previous year
Jan 1 星期五Jan 1 Fri 第1周Week 1 上一年的最後一周Last week of the previous year 上一年的最後一周Last week of the previous year
Jan 2 SatJan 2 Sat 第1周Week 1 上一年的最後一周Last week of the previous year 上一年的最後一周Last week of the previous year
Jan 3 太陽Jan 3 Sun 第2周Week 2 第1周Week 1 第1周Week 1
Jan 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
Jan 8 日星期五Jan 8 Fri 第2周Week 2 第1周Week 1 第1周Week 1
Jan 9 SatJan 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日)則屬於前一年的 50-3 周。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日)定義為一年第一周的開頭,因為一月6年1月1日星期二和星期日之間有超過四天的時間。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,做為一周第一周的第一天,在一年第一周的第一周開始時,因為2010中的第一個星期是1月3日到9日。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 中的 blog 專案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. Calendar 屬性會傳回文化特性的預設行事曆,而 OptionalCalendars 屬性會傳回陣列,其中包含文化特性支援的所有行事曆。The 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所使用的行事曆,請將 CultureInfo.DateTimeFormatCalendar 屬性設定為新的 CalendarTo change the calendar used by a CultureInfo, set the Calendar property of CultureInfo.DateTimeFormat to a new Calendar.

適用於

另請參閱