CalendarWeekRule CalendarWeekRule CalendarWeekRule CalendarWeekRule Enum

定義

定義決定年份的第一週的各種規則 (Rule)。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. 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.

比方說,如果在西曆中,假設 year (年 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 日到下列前一天星期日。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 日星期二Jan 1 Tue 第 1 週Week 1 前一年的最後一週Last week of the previous year 第 1 週Week 1
年 1 月 2日日星期三Jan 2 Wed 第 1 週Week 1 前一年的最後一週Last week of the previous year 第 1 週Week 1
年 1 月 3 日,星期四Jan 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日 SatJan 5 Sat 第 1 週Week 1 前一年的最後一週Last week of the previous year 第 1 週Week 1
年 1 月 6 SunJan 6 Sun 第 2 週Week 2 第 1 週Week 1 第 2 週Week 2
年 1 月 7 日星期一Jan 7 Mon 第 2 週Week 2 第 1 週Week 1 第 2 週Week 2
年 1 月 8 週二Jan 8 Tue 第 2 週Week 2 第 1 週Week 1 第 2 週Week 2
年 1 月 9日日星期三Jan 9 Wed 第 2 週Week 2 第 1 週Week 1 第 2 週Week 2
年 1 月 10 日,星期四Jan 10 Thu 第 2 週Week 2 第 1 週Week 1 第 2 週Week 2
年 1 月 11 日星期五 03:52:31Jan 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 年) 一年的第一天落在星期五,一週指定的第一天是星期日。Suppose the first day of the year (January 1) falls on a Friday and the designated first day of the week is Sunday. 因為有少於四天年 1 月 1 日到下列前一天,選取 FirstFourDayWeek 定義第一個星期日 (1 月 3) 年的第一週的開頭為星期日。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
年 1 月 1 日星期五Jan 1 Fri 第 1 週Week 1 前一年的最後一週Last week of the previous year 前一年的最後一週Last week of the previous year
年 1 月 2日 SatJan 2 Sat 第 1 週Week 1 前一年的最後一週Last week of the previous year 前一年的最後一週Last week of the previous year
年 1 月 3 SunJan 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
年 1 月 5 個週二Jan 5 Tue 第 2 週Week 2 第 1 週Week 1 第 1 週Week 1
年 1 月 6日日星期三Jan 6 Wed 第 2 週Week 2 第 1 週Week 1 第 1 週Week 1
年 1 月 7 日,星期四Jan 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
年 1 月 9日日星期六Jan 9 Sat 第 2 週Week 2 第 1 週Week 1 第 1 週Week 1

下列範例說明如何FirstDaySystem.DayOfWeek值會一起用來判斷如何會指派週。The following example illustrates how the FirstDay and System.DayOfWeek values are used together to determine how weeks are assigned. 西曆,(1 年) 一年在 2013年中的第一天落在星期二。In the Gregorian calendar, the first day of the year (January 1) in 2013 falls on a Tuesday. 如果一週指定的第一天是星期日,第一個星期日 (1 月 6) 是年度的第一週的第一天和星期六 (年 1 月 5) 屬於前一年的五千萬第三週。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 日星期二與星期日,月 6 日之間。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 規則System.DayOfWeek與第一個的一週天數使年 1 月 3,星期日年度的第一週的開頭因為 2010年中有多個四天的第一週是 Janu元 3 到 9。For 2010, a year in which January 1 falls on a Friday, applying the FirstFourDayWeek rule with System.DayOfWeek 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. 部落格文章中討論的差異ISO 8601 週年中的格式,在 Microsoft.NETThe differences are discussed in the blog entry ISO 8601 Week of Year format in Microsoft .NET. 開始使用.NET Core 3.0ISOWeek.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,設定應用程式應該Calendar屬性CultureInfo.DateTimeFormat到新CalendarWeekRule.FirstFourDayWeekTo change the calendar used by a CultureInfo, the application should set the Calendar property of CultureInfo.DateTimeFormat to a new CalendarWeekRule.FirstFourDayWeek.

適用於

另請參閱