TaiwanCalendar.AddYears(DateTime, Int32) Methode

Definition

Gibt eine DateTime zurück, die dem angegebenen Zeitintervall zur angegebenen DateTime in Jahren entspricht.

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

Parameter

time
DateTime

Die DateTime, der Jahre hinzugefügt werden sollen.

years
Int32

Die Anzahl der hinzuzufügenden Jahre

Gibt zurück

Die DateTime, die sich ergibt, wenn der angegebenen DateTime die angegebene Anzahl von Jahren hinzugefügt wird.

Ausnahmen

Die resultierende DateTime liegt außerhalb des unterstützten Bereichs.

Beispiele

Im folgenden Codebeispiel wird die Verwendung der AddYears -Methode angezeigt.

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 TaiwanCalendar.
   TaiwanCalendar^ myCal = gcnew TaiwanCalendar;
   
   // Displays the values of the DateTime.
   Console::WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Taiwan 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 Taiwan calendar:
   Era:        1
   Year:       91
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        1
   Year:       94
   Month:      2
   DayOfYear:  34
   DayOfMonth: 3
   DayOfWeek:  Thursday

*/
using System;
using System.Globalization;

public class SamplesTaiwanCalendar  {

   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 TaiwanCalendar.
      TaiwanCalendar myCal = new TaiwanCalendar();

      // Displays the values of the DateTime.
      Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Taiwan 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 Taiwan calendar:
   Era:        1
   Year:       91
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        1
   Year:       94
   Month:      2
   DayOfYear:  34
   DayOfMonth: 3
   DayOfWeek:  Thursday

*/
Imports System.Globalization


Public Class SamplesTaiwanCalendar   

   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 TaiwanCalendar.
      Dim myCal As New TaiwanCalendar()

      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Taiwan 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 Taiwan calendar:
'   Era:        1
'   Year:       91
'   Month:      4
'   DayOfYear:  93
'   DayOfMonth: 3
'   DayOfWeek:  Wednesday
'
'After adding two years and ten months:
'   Era:        1
'   Year:       94
'   Month:      2
'   DayOfYear:  34
'   DayOfMonth: 3
'   DayOfWeek:  Thursday

Hinweise

Der Tagesteil des resultierenden DateTime ist betroffen, wenn der resultierende Tag kein gültiger Tag im resultierenden Monat des resultierenden Jahres ist. Es wird in den letzten gültigen Tag im resultierenden Monat des resultierenden Jahres geändert. Der Monatsteil des resultierenden DateTime bleibt identisch mit dem angegebenen DateTime. Diese Implementierung unterstützt nur den aktuellen Zeitraum. Daher wird ausgelöst, ArgumentException wenn das resultierende Jahr außerhalb des Zeitraums des angegebenen DateTimeliegt. Der Tageszeitteil des resultierenden DateTime bleibt identisch mit dem angegebenen DateTime.

Beispielsweise hat Februar 28 Tage, außer in Schaltjahren, wenn er 29 Tage hat. Wenn das angegebene Datum der 29. Tag des Februars in einem Schaltjahr ist und der Wert von years 1 ist, ist das resultierende Datum der 28. Tag des Februars im folgenden Jahr.

Wenn years negativ ist, ist das ergebnis DateTime vor dem angegebenen DateTime.

Die Kind -Eigenschaft des zurückgegebenen DateTime Werts ist DateTimeKind.Unspecifiedimmer gleich . Sie können die Kind -Eigenschaft des time Parameters beibehalten, indem Sie die DateTime.SpecifyKind -Methode aufrufen, wie im folgenden Beispiel gezeigt.

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

Gilt für:

Weitere Informationen