Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) Method

Определение

Возвращает неделю года, к которой относится дата в заданном значении DateTime.Returns the week of the year that includes the date in the specified DateTime value.

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

Значение даты и времени.A date and time value.

rule
CalendarWeekRule CalendarWeekRule CalendarWeekRule CalendarWeekRule

Значение перечисления, определяющее календарную неделю.An enumeration value that defines a calendar week.

firstDayOfWeek
DayOfWeek DayOfWeek DayOfWeek DayOfWeek

Значение перечисления, представляющее первый день недели.An enumeration value that represents the first day of the week.

Возвраты

Положительное целое число, представляющее неделю года, к которой относится дата в параметре time.A positive integer that represents the week of the year that includes the date in the time parameter.

Исключения

Время, указанное в параметре time, наступает раньше момента времени, определенного свойством MinSupportedDateTime, или позже момента времени, определенного свойством MaxSupportedDateTime.time is earlier than MinSupportedDateTime or later than MaxSupportedDateTime.

-или--or- firstDayOfWeek не является допустимым значением DayOfWeek.firstDayOfWeek is not a valid DayOfWeek value.

- или --or- rule не является допустимым значением CalendarWeekRule.rule is not a valid CalendarWeekRule value.

Примеры

В следующем примере кода показано, как результат GetWeekOfYear зависит от FirstDayOfWeek CalendarWeekRule используемого объекта и.The following code example shows how the result of GetWeekOfYear varies depending on the FirstDayOfWeek and the CalendarWeekRule used. Если указанная дата является последним днем года, GetWeekOfYear возвращает общее количество недель в этом году.If the specified date is the last day of the year, GetWeekOfYear returns the total number of weeks in that year.

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 'SamplesCalendar


'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 значение последний день года.This method can be used to determine the number of weeks in the year by setting time to the last day of the year.

Объект для определенного языка и региональных параметров, использующих календарь, DateTimeFormatInfo.Calendar указанный свойством, содержит следующие значения, зависящие от языка и региональных rule параметров, которые можно использовать для параметров и firstDayOfWeek: DateTimeFormatInfoThe DateTimeFormatInfo object for a particular culture that uses the calendar indicated by the DateTimeFormatInfo.Calendar property includes the following culture-specific values that can be used for the rule and firstDayOfWeek parameters:

  • Свойство содержит первый день недели по умолчанию, который может использоваться firstDayOfWeek для параметра. DateTimeFormatInfo.FirstDayOfWeekThe DateTimeFormatInfo.FirstDayOfWeek property contains the default first day of the week that can be used for the firstDayOfWeek parameter.

  • Свойство содержит правило календарной недели по умолчанию, которое может использоваться rule для параметра. DateTimeFormatInfo.CalendarWeekRuleThe DateTimeFormatInfo.CalendarWeekRule property contains the default calendar week rule that can be used for the rule parameter.

В следующем примере DateTimeFormatInfo объект текущего языка и региональных параметров используется для определения того, что 1 января 2011 в качестве первого дня года в григорианском календаре.The following example uses the current culture's DateTimeFormatInfo object to determine that January 1, 2011 is in the first week of the year in the Gregorian calendar.

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 метода ArgumentOutOfRangeException создает исключение для определенных сочетаний rule значений и firstDayOfWeek , даже MinSupportedDateTime если time значение больше даты, возвращенной свойством этого календаря.For some calendars, a call to the GetWeekOfYear method throws an ArgumentOutOfRangeException for particular combinations of rule and firstDayOfWeek values even if time is greater than the date returned by that calendar's MinSupportedDateTime property. В следующей таблице перечислены затронутые календари, rule конкретные значения и диапазон наиболее ранних поддерживаемых time значений.The following table lists the affected calendars, the specific rule values, and the range of the earliest supported time values. Определенное минимальное DateTime значение зависит от значения firstDayOfWeek параметра.The specific minimum DateTime value depends on the value of the firstDayOfWeek parameter.

КалендарьCalendar Значение КалендарвикрулеCalendarWeekRule value По григорианскому календарю (M/дд/гггг)Gregorian date (M/dd/yyyy) Дата в календаре (м/дд/гггг)Date in calendar (M/dd/yyyy)
ChineseLunisolarCalendar FirstFullWeek от 2/19/1901 до 2/25/19012/19/1901 to 2/25/1901 от 1/1/1901 до 1/7/19011/1/1901 to 1/7/1901
ChineseLunisolarCalendar FirstFourDayWeek от 2/19/1901 до 2/22/19012/19/1901 to 2/22/1901 от 1/01/1901 до 1/04/19011/01/1901 to 1/04/1901
HebrewCalendar FirstDay 9/17/15839/17/1583 1/01/53441/01/5344
HebrewCalendar FirstFullWeek от 9/17/1583 до 9/23/15839/17/1583 to 9/23/1583 от 1/01/5344 до 1/07/53441/01/5344 to 1/07/5344
HebrewCalendar FirstFourDayWeek от 9/17/1583 до 9/20/15839/17/1583 to 9/20/1583 от 1/01/5344 до 1/04/53441/01/5344 to 1/04/5344
HijriCalendar FirstFullWeek от 7/18/0622 до 7/24/06227/18/0622 to 7/24/0622 от 1/01/0001 до 1/07/00011/01/0001 to 1/07/0001
HijriCalendar FirstFourDayWeek от 7/18/0622 до 7/21/06227/18/0622 to 7/21/0622 от 1/01/0001 до 1/04/00011/01/0001 to 1/04/0001
JapaneseLunisolarCalendar FirstFullWeek от 1/28/1960 до 2/03/19601/28/1960 to 2/03/1960 от 1/01/35 до 1/07/00351/01/35 to 1/07/0035
JapaneseLunisolarCalendar FirstFourDayWeek от 1/28/1960 до 1/31/19601/28/1960 to 1/31/1960 от 1/01/0035 до 1/04/00351/01/0035 to 1/04/0035
JulianCalendar FirstFullWeek от 1/01/0001 до 1/05/00011/01/0001 to 1/05/0001 от 1/03/0001 до 1/07/00011/03/0001 to 1/07/0001
JulianCalendar FirstFourDayWeek от 1/01/0001 до 1/02/00011/01/0001 to 1/02/0001 от 1/03/0001 до 1/04/00011/03/0001 to 1/04/0001
KoreanLunisolarCalendar FirstFullWeek от 2/14/0918 до 2/20/09182/14/0918 to 2/20/0918 от 1/01/0918 до 1/07/09181/01/0918 to 1/07/0918
KoreanLunisolarCalendar FirstFourDayWeek от 2/14/0918 до 2/17/09182/14/0918 to 2/17/0918 от 1/01/0918 до 1/04/09181/01/0918 to 1/04/0918
PersianCalendar FirstFullWeek от 3/21/0622 до 3/27/06223/21/0622 to 3/27/0622 от 1/01/0001 до 1/07/00011/01/0001 to 1/07/0001
PersianCalendar FirstFourDayWeek от 3/21/0622 до 3/24/06223/21/0622 to 3/24/0622 от 1/01/0001 до 1/04/00011/01/0001 to 1/04/0001
TaiwanLunisolarCalendar FirstFullWeek от 2/18/1912 до 2/24/19122/18/1912 to 2/24/1912 от 1/01/0001 до 1/07/00011/01/0001 to 1/07/0001
TaiwanLunisolarCalendar FirstFourDayWeek от 2/18/1912 до 2/21/19122/18/1912 to 2/21/1912 от 1/01/0001 до 1/04/00011/01/0001 to 1/04/0001
UmAlQuraCalendar FirstFullWeek от 4/30/1900 до 5/06/19004/30/1900 to 5/06/1900 от 1/01/1318 до 1/07/13181/01/1318 to 1/07/1318
UmAlQuraCalendar FirstFourDayWeek от 4/30/1900 до 5/03/19004/30/1900 to 5/03/1900 от 1/01/1318 до 1/04/13181/01/1318 to 1/04/1318

Применяется к

Дополнительно