Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) 메서드

정의

지정된 DateTime 값의 날짜가 포함된 주(연도 기준)를 반환합니다.

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

날짜 및 시간 값입니다.

rule
CalendarWeekRule

주를 정의하는 열거형 값입니다.

firstDayOfWeek
DayOfWeek

주의 첫째 요일을 정의하는 열거형 값입니다.

반환

time 매개 변수의 날짜가 포함된 주(연도 기준)를 나타내는 양의 정수입니다.

예외

timeMinSupportedDateTime보다 이전이거나 MaxSupportedDateTime보다 이후인 경우

또는

firstDayOfWeek는 유효한 DayOfWeek 값이 아닙니다.

또는

rule는 유효한 CalendarWeekRule 값이 아닙니다.

예제

다음 코드 예제에서는 의 결과가 GetWeekOfYear 및 사용된 에 FirstDayOfWeekCalendarWeekRule 따라 어떻게 달라지는지 보여 줍니다. 지정된 날짜가 연도의 마지막 날인 경우 는 해당 연도 GetWeekOfYear 의 총 주 수를 반환합니다.

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 하여 연도의 주 수를 결정하는 데 사용할 수 있습니다.

DateTimeFormatInfo 속성으로 표시된 달력을 사용하는 특정 문화권의 DateTimeFormatInfo.Calendar 개체에는 및 firstDayOfWeek 매개 변수에 사용할 수 있는 다음과 같은 문화권별 값이 rule 포함됩니다.

참고

ISO 8601에 정확히 매핑되지 않습니다. 차이점은 Microsoft .NET의 ISO 8601 Week of Year 형식 블로그 항목에서 설명합니다. .NET Core 3.0 ISOWeek.GetYearISOWeek.GetWeekOfYear 부터 이 문제를 해결합니다.

다음 예제에서는 현재 문화권의 DateTimeFormatInfo 개체를 사용하여 2011년 1월 1일이 그레고리오력의 첫 번째 주에 있음을 확인합니다.

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 가 해당 달력의 rule 속성에서 반환한 ArgumentOutOfRangeException 날짜보다 크더라도 timefirstDayOfWeek 값의 MinSupportedDateTime 특정 조합에 대해 이 throw됩니다. 다음 표에서는 영향을 받는 달력, 특정 rule 값 및 지원되는 time 가장 빠른 값의 범위를 나열합니다. 특정 최소 DateTime 값은 매개 변수의 firstDayOfWeek 값에 따라 달라집니다.

달력 CalendarWeekRule 값 그레고리오 날짜(M/dd/yyyy) 달력의 날짜(M/dd/yyyy)
ChineseLunisolarCalendar FirstFullWeek 1901년 2월 19일~1901년 2월 25일 1901년 1월 1일~1901년 1월 7일
ChineseLunisolarCalendar FirstFourDayWeek 1901년 2월 19일~1901년 2월 22일 1901/1/01~1901/1/04
HebrewCalendar FirstDay 9/17/1583 1/01/5344
HebrewCalendar FirstFullWeek 1583년 9월 17일 ~ 1583년 9월 23일 1/01/5344 ~ 1/07/5344
HebrewCalendar FirstFourDayWeek 1583년 9월 17일 ~1583년 9월 20일 1/01/5344 ~ 1/04/5344
HijriCalendar FirstFullWeek 7/18/0622 ~ 7/24/0622 1/01/0001 ~1/07/0001
HijriCalendar FirstFourDayWeek 7/18/0622 ~ 7/21/0622 1/01/0001 ~1/04/0001
JapaneseLunisolarCalendar FirstFullWeek 1960년 1월 28일 ~1960년 2월 3일 1/01/35 ~ 1/07/0035
JapaneseLunisolarCalendar FirstFourDayWeek 1960년 1월 28일 ~1960년 1월 31일 1/01/0035 ~ 1/04/0035
JulianCalendar FirstFullWeek 1/01/0001 ~1/05/0001 1/03/0001 ~1/07/0001
JulianCalendar FirstFourDayWeek 1/01/0001 ~1/02/0001 1/03/0001 ~1/04/0001
KoreanLunisolarCalendar FirstFullWeek 2/14/0918 ~2/20/0918 1/01/0918 ~1/07/0918
KoreanLunisolarCalendar FirstFourDayWeek 2/14/0918 ~2/17/0918 1/01/0918 ~1/04/0918
PersianCalendar FirstFullWeek 3/21/0622 ~ 3/27/0622 1/01/0001 ~1/07/0001
PersianCalendar FirstFourDayWeek 3/21/0622 ~ 3/24/0622 1/01/0001 ~1/04/0001
TaiwanLunisolarCalendar FirstFullWeek 1912년 2월 18일~1912년 2월 24일 1/01/0001 ~1/07/0001
TaiwanLunisolarCalendar FirstFourDayWeek 1912년 2월 18일 ~1912년 2월 21일 1/01/0001 ~1/04/0001
UmAlQuraCalendar FirstFullWeek 1900년 4월 30일 ~1900년 5월 6일 1/01/1318 ~1/07/1318
UmAlQuraCalendar FirstFourDayWeek 1900년 4월 30일 ~1900년 5월 33일 1/01/1318 ~1/04/1318

적용 대상

추가 정보