Muokkaa

Jaa


CalendarWeekRule Enum

Definition

Defines different rules for determining the first week of the year.

public enum class CalendarWeekRule
public enum CalendarWeekRule
[System.Serializable]
public enum CalendarWeekRule
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CalendarWeekRule
type CalendarWeekRule = 
[<System.Serializable>]
type CalendarWeekRule = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CalendarWeekRule = 
Public Enum CalendarWeekRule
Inheritance
CalendarWeekRule
Attributes

Fields

Name Value Description
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. The value is 0.

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. The value is 1.

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. The value is 2.

Remarks

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. The enumeration value is also used as a parameter to the Calendar.GetWeekOfYear method.

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

The first week based on the FirstDay value can have one to seven days. The first week based on the FirstFullWeek value always has seven days. The first week based on the FirstFourDayWeek value can have four to seven days.

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. 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. 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 FirstDay FirstFullWeek FirstFourDayWeek
Dec 31 Mon Last week of the previous year Last week of the previous year Last week of the previous year
Jan 1 Tue Week 1 Last week of the previous year Week 1
Jan 2 Wed Week 1 Last week of the previous year Week 1
Jan 3 Thu Week 1 Last week of the previous year Week 1
Jan 4 Fri Week 1 Last week of the previous year Week 1
Jan 5 Sat Week 1 Last week of the previous year Week 1
Jan 6 Sun Week 2 Week 1 Week 2
Jan 7 Mon Week 2 Week 1 Week 2
Jan 8 Tue Week 2 Week 1 Week 2
Jan 9 Wed Week 2 Week 1 Week 2
Jan 10 Thu Week 2 Week 1 Week 2
Jan 11 Fri Week 2 Week 1 Week 2
Jan 12 Sat Week 2 Week 1 Week 2

Suppose the first day of the year (January 1) falls on a Friday and the designated first day of the week is Sunday. 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 FirstDay FirstFullWeek FirstFourDayWeek
Dec 31 Thu Last week of the previous year Last week of the previous year Last week of the previous year
Jan 1 Fri Week 1 Last week of the previous year Last week of the previous year
Jan 2 Sat Week 1 Last week of the previous year Last week of the previous year
Jan 3 Sun Week 2 Week 1 Week 1
Jan 4 Mon Week 2 Week 1 Week 1
Jan 5 Tue Week 2 Week 1 Week 1
Jan 6 Wed Week 2 Week 1 Week 1
Jan 7 Thu Week 2 Week 1 Week 1
Jan 8 Fri Week 2 Week 1 Week 1
Jan 9 Sat Week 2 Week 1 Week 1

The following example illustrates how the CalendarWeekRule and DayOfWeek values are used together to determine how weeks are assigned. In the Gregorian calendar, the first day of the year (January 1) in 2013 falls on a Tuesday. 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. 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. Using this rule, January 5 belongs to the first week of the year. 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

Note

This does not map exactly to ISO 8601. The differences are discussed in the blog entry ISO 8601 Week of Year format in Microsoft .NET. Starting with .NET Core 3.0, ISOWeek.GetYear and ISOWeek.GetWeekOfYear solve this problem.

Each CultureInfo object supports a set of calendars. 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. To change the calendar used by a CultureInfo, set the Calendar property of CultureInfo.DateTimeFormat to a new Calendar.

Applies to

See also