HebrewCalendar.AddYears(DateTime, Int32) 方法

定義

傳回與指定 DateTime 相差指定年數的 DateTimeReturns a DateTime that is the specified number of years away from the specified DateTime.

public:
 override DateTime AddYears(DateTime time, int years);
public override DateTime AddYears (DateTime time, int years);
override this.AddYears : DateTime * int -> DateTime
Public Overrides Function AddYears (time As DateTime, years As Integer) As DateTime

參數

time
DateTime

要將 DateTime 加入的 yearsThe DateTime to which to add years.

years
Int32

要加入的年數。The number of years to add.

傳回

DateTime,由將指定的年數加入指定的 DateTime 所產生。The DateTime that results from adding the specified number of years to the specified DateTime.

例外狀況

產生的 DateTime 在支援的範圍之外。The resulting DateTime is outside the supported range.

範例

下列程式碼範例會根據希伯來文行事曆顯示 DateTime 的數個元件的值。The following code example displays the values of several components of a DateTime in terms of the Hebrew calendar.

using namespace System;
using namespace System::Globalization;
void DisplayValues( Calendar^ myCal, DateTime myDT )
{
   Console::WriteLine( "   Era:        {0}", myCal->GetEra( myDT ) );
   Console::WriteLine( "   Year:       {0}", myCal->GetYear( myDT ) );
   Console::WriteLine( "   Month:      {0}", myCal->GetMonth( myDT ) );
   Console::WriteLine( "   DayOfYear:  {0}", myCal->GetDayOfYear( myDT ) );
   Console::WriteLine( "   DayOfMonth: {0}", myCal->GetDayOfMonth( myDT ) );
   Console::WriteLine( "   DayOfWeek:  {0}", myCal->GetDayOfWeek( myDT ) );
   Console::WriteLine();
}

int main()
{
   
   // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
   DateTime myDT = DateTime(2002,4,3,gcnew GregorianCalendar);
   
   // Creates an instance of the HebrewCalendar.
   HebrewCalendar^ myCal = gcnew HebrewCalendar;
   
   // Displays the values of the DateTime.
   Console::WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:" );
   DisplayValues( myCal, myDT );
   
   // Adds two years and ten months.
   myDT = myCal->AddYears( myDT, 2 );
   myDT = myCal->AddMonths( myDT, 10 );
   
   // Displays the values of the DateTime.
   Console::WriteLine( "After adding two years and ten months:" );
   DisplayValues( myCal, myDT );
}

/*
This code produces the following output.

April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:
   Era:        1
   Year:       5762
   Month:      7
   DayOfYear:  198
   DayOfMonth: 21
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        1
   Year:       5765
   Month:      5
   DayOfYear:  138
   DayOfMonth: 21
   DayOfWeek:  Monday

*/
using System;
using System.Globalization;

public class SamplesHebrewCalendar  {

   public static void Main()  {

      // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
      DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );

      // Creates an instance of the HebrewCalendar.
      HebrewCalendar myCal = new HebrewCalendar();

      // Displays the values of the DateTime.
      Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:" );
      DisplayValues( myCal, myDT );

      // Adds two years and ten months.
      myDT = myCal.AddYears( myDT, 2 );
      myDT = myCal.AddMonths( myDT, 10 );

      // Displays the values of the DateTime.
      Console.WriteLine( "After adding two years and ten months:" );
      DisplayValues( myCal, myDT );
   }

   public static void DisplayValues( Calendar myCal, DateTime myDT )  {
      Console.WriteLine( "   Era:        {0}", myCal.GetEra( myDT ) );
      Console.WriteLine( "   Year:       {0}", myCal.GetYear( myDT ) );
      Console.WriteLine( "   Month:      {0}", myCal.GetMonth( myDT ) );
      Console.WriteLine( "   DayOfYear:  {0}", myCal.GetDayOfYear( myDT ) );
      Console.WriteLine( "   DayOfMonth: {0}", myCal.GetDayOfMonth( myDT ) );
      Console.WriteLine( "   DayOfWeek:  {0}", myCal.GetDayOfWeek( myDT ) );
      Console.WriteLine();
   }
}

/*
This code produces the following output.

April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:
   Era:        1
   Year:       5762
   Month:      7
   DayOfYear:  198
   DayOfMonth: 21
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        1
   Year:       5765
   Month:      5
   DayOfYear:  138
   DayOfMonth: 21
   DayOfWeek:  Monday

*/
Imports System.Globalization


Public Class SamplesHebrewCalendar   

   Public Shared Sub Main()

      ' Sets a DateTime to April 3, 2002 of the Gregorian calendar.
      Dim myDT As New DateTime(2002, 4, 3, New GregorianCalendar())

      ' Creates an instance of the HebrewCalendar.
      Dim myCal As New HebrewCalendar()

      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:")
      DisplayValues(myCal, myDT)

      ' Adds two years and ten months.
      myDT = myCal.AddYears(myDT, 2)
      myDT = myCal.AddMonths(myDT, 10)

      ' Displays the values of the DateTime.
      Console.WriteLine("After adding two years and ten months:")
      DisplayValues(myCal, myDT)

   End Sub

   Public Shared Sub DisplayValues(myCal As Calendar, myDT As DateTime)
      Console.WriteLine("   Era:        {0}", myCal.GetEra(myDT))
      Console.WriteLine("   Year:       {0}", myCal.GetYear(myDT))
      Console.WriteLine("   Month:      {0}", myCal.GetMonth(myDT))
      Console.WriteLine("   DayOfYear:  {0}", myCal.GetDayOfYear(myDT))
      Console.WriteLine("   DayOfMonth: {0}", myCal.GetDayOfMonth(myDT))
      Console.WriteLine("   DayOfWeek:  {0}", myCal.GetDayOfWeek(myDT))
      Console.WriteLine()
   End Sub

End Class



'This code produces the following output.

'

'April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:

'   Era:        1

'   Year:       5762

'   Month:      7

'   DayOfYear:  198

'   DayOfMonth: 21

'   DayOfWeek:  Wednesday

'

'After adding two years and ten months:

'   Era:        1

'   Year:       5765

'   Month:      5

'   DayOfYear:  138

'   DayOfMonth: 21

'   DayOfWeek:  Monday


備註

HebrewCalendar 類別的執行只會辨識希伯來文年份5343到5999(西曆中的1583到2239)。This implementation of the HebrewCalendar class recognizes only the Hebrew years 5343 to 5999 (1583 to 2239 in the Gregorian calendar).

如果產生的 DateTime 中的日部分不是產生的年份結果月份中的有效日,則會影響所產生之的天數。The day part of the resulting DateTime is affected if the resulting day is not a valid day in the resulting month of the resulting year. 在產生的年份中,它會變更為最後一個有效的日期。It is changed to the last valid day in the resulting month of the resulting year. 例如,Cheshvan 可以有29或30天,視 Jewish 假日的位置而定。For example, Cheshvan can have 29 or 30 days, depending on the placement of Jewish holidays. 假設 Cheshvan 在目前的年份為30天,而在下一年則為29。Suppose that Cheshvan has 30 days in the current year and 29 in the following year. 如果指定的日期是目前年份中 Cheshvan 的30天,且 years 的值為1,則產生的日期會是下一年 Cheshvan 的第29天。If the specified date is the 30th day of Cheshvan in the current year and the value of years is 1, the resulting date will be the 29th day of Cheshvan in the following year.

如果產生的月份不是產生的年份中的有效月份,則產生之 DateTime 的月份部分會受到影響。The month part of the resulting DateTime is affected if the resulting month is not a valid month in the resulting year. 它會變更為所產生年份中的最後一個有效月份。It is changed to the last valid month in the resulting year. 例如,如果 time 參數中的月份是閏年的第13個月,而 years 的值是1,則產生的 DateTime 中的月份就是下一年的第12個月,也就是非閏年。For example, if the month in the time parameter is the 13th month of a leap year and the value of years is 1, the month in the resulting DateTime is the 12th month of the following year, which is a non-leap year. 請注意,即使月份部分不會變更,它仍可能參考不同的月份。Note that even when the month part does not change, it might still refer to a different month. 例如,Adar Beit 是閏年的第七個月,但是 Nissan 是常見年份的第七個月。For example, Adar Beit is the 7th month in leap years, but Nissan is the 7th month in common years.

此實作為僅支援目前的紀元。This implementation supports only the current era. 因此,如果產生的年份超出指定 DateTime的紀元,則會擲回 ArgumentExceptionTherefore, ArgumentException is thrown if the resulting year is outside the era of the specified DateTime.

產生之 DateTime 的日期時間部分與指定的 DateTime保持一致。The time-of-day part of the resulting DateTime remains the same as the specified DateTime.

如果 years 為負數,則產生的 DateTime 早于指定的 DateTimeIf years is negative, the resulting DateTime is earlier than the specified DateTime.

傳回之 DateTime 值的 Kind 屬性一律等於 DateTimeKind.UnspecifiedThe Kind property of the returned DateTime value always equals DateTimeKind.Unspecified. 您可以藉由呼叫 DateTime.SpecifyKind 方法來保留 time 參數的 Kind 屬性,如下列範例所示。You can preserve the Kind property of the time parameter by calling the DateTime.SpecifyKind method, as the following example shows.

returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind)

適用於

另請參閱