KoreanCalendar.AddYears(DateTime, Int32) KoreanCalendar.AddYears(DateTime, Int32) KoreanCalendar.AddYears(DateTime, Int32) KoreanCalendar.AddYears(DateTime, Int32) Method

Definizione

Restituisce un valore DateTime che rappresenta il numero di anni specificato a partire dal valore DateTime specificato.Returns 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

Parametri

time
DateTime DateTime DateTime DateTime

Oggetto DateTime a cui aggiungere anni.The DateTime to which to add years.

years
Int32 Int32 Int32 Int32

Numero di anni da aggiungere.The number of years to add.

Restituisce

Valore DateTime risultante dalla somma del numero di anni specificato e del valore DateTime specificato.The DateTime that results from adding the specified number of years to the specified DateTime.

Eccezioni

years o time non è compreso nell'intervallo consentito.years or time is out of range.

Esempi

Esempio di codice seguente consente di visualizzare i valori dei diversi componenti di un DateTime per il calendario coreano.The following code example displays the values of several components of a DateTime in terms of the Korean 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 KoreanCalendar.
   KoreanCalendar^ myCal = gcnew KoreanCalendar;
   
   // Displays the values of the DateTime.
   Console::WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Korean 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 Korean calendar:
   Era:        1
   Year:       4335
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

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

*/
using System;
using System.Globalization;


public class SamplesKoreanCalendar  {

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

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

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

*/
Imports System
Imports System.Globalization


Public Class SamplesKoreanCalendar   

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

      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Korean 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 'Main

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

End Class 'SamplesKoreanCalendar 


'This code produces the following output.

'

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

'   Era:        1

'   Year:       4335

'   Month:      4

'   DayOfYear:  93

'   DayOfMonth: 3

'   DayOfWeek:  Wednesday

'

'After adding two years and ten months:

'   Era:        1

'   Year:       4338

'   Month:      2

'   DayOfYear:  34

'   DayOfMonth: 3

'   DayOfWeek:  Thursday


Commenti

La parte del giorno dell'oggetto risultante DateTime è interessato se il giorno risulta non è un giorno valido nel mese dell'anno risulta risulta.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. Verrà utilizzato l'ultimo giorno del mese dell'anno risulta risultante valido.It is changed to the last valid day in the resulting month of the resulting year. La parte del mese del valore risultante DateTime rimangono invariati rispetto alle specificato DateTime.The month part of the resulting DateTime remains the same as the specified DateTime. Questa implementazione supporta solo l'era corrente.This implementation supports only the current era. Pertanto ArgumentException viene generata un'eccezione se l'anno risulta non rientra nell'era dell'oggetto specificato DateTime.Therefore, ArgumentException is thrown if the resulting year is outside the era of the specified DateTime. La parte ora del giorno dell'oggetto risultante DateTime rimangono invariati rispetto alle specificato DateTime.The time-of-day part of the resulting DateTime remains the same as the specified DateTime.

Ad esempio, febbraio ha 28 giorni, eccetto durante gli anni bisestili quando ha 29 giorni.For example, February has 28 days, except during leap years when it has 29 days. Se la data specificata è 29 ° giorno del mese di febbraio in un anno bisestile e il valore di years è 1, la data risulta è il giorno 28 febbraio nell'anno successivo.If the specified date is the 29th day of February in a leap year and the value of years is 1, the resulting date is the 28th day of February in the following year.

Se years è negativo, l'oggetto risultante DateTime precedente a specificato DateTime.If years is negative, the resulting DateTime is earlier than the specified DateTime.

Il Kind proprietà dell'oggetto restituito DateTime sempre valore è uguale a DateTimeKind.Unspecified.The Kind property of the returned DateTime value always equals DateTimeKind.Unspecified. È possibile mantenere il Kind proprietà del time parametro chiamando il DateTime.SpecifyKind metodo, come illustrato nell'esempio seguente.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)

Si applica a

Vedi anche