GregorianCalendar.AddYears(DateTime, Int32) Méthode

Définition

Retourne un DateTime qui est le nombre d’années spécifié jusqu’au DateTime spécifié.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

Paramètres

time
DateTime

DateTime auquel ajouter des années.The DateTime to which to add years.

years
Int32

Nombre d’années à ajouter.The number of years to add.

Retours

DateTime qui résulte de l’ajout du nombre d’années spécifié au DateTime spécifié.The DateTime that results from adding the specified number of years to the specified DateTime.

Exceptions

Le DateTime résultant se trouve en dehors de la plage prise en charge.The resulting DateTime is outside the supported range.

Exemples

L’exemple de code suivant affiche les valeurs de plusieurs composants d’un DateTime en termes de calendrier grégorien.The following code example displays the values of several components of a DateTime in terms of the Gregorian 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 GregorianCalendar.
   GregorianCalendar^ myCal = gcnew GregorianCalendar;
   
   // Displays the values of the DateTime.
   Console::WriteLine( "April 3, 2002 of the Gregorian 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:
   Era:        1
   Year:       2002
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

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

*/
using System;
using System.Globalization;

public class SamplesGregorianCalendar  {

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

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

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

*/
Imports System.Globalization


Public Class SamplesGregorianCalendar   

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

      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian 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:

'   Era:        1

'   Year:       2002

'   Month:      4

'   DayOfYear:  93

'   DayOfMonth: 3

'   DayOfWeek:  Wednesday

'

'After adding two years and ten months:

'   Era:        1

'   Year:       2005

'   Month:      2

'   DayOfYear:  34

'   DayOfMonth: 3

'   DayOfWeek:  Thursday


Remarques

La partie jour du DateTime résultant est affectée si le jour résultant n’est pas un jour valide dans le mois résultant de l’année résultante.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. Il est remplacé par le dernier jour valide dans le mois résultant de l’année résultante.It is changed to the last valid day in the resulting month of the resulting year. La partie mois du DateTime résultant reste la même que la DateTimespécifiée.The month part of the resulting DateTime remains the same as the specified DateTime. Cette implémentation prend en charge uniquement l’ère actuelle.This implementation supports only the current era. Par conséquent, ArgumentException est levée si l’année obtenue est en dehors de l’ère du DateTimespécifié.Therefore, ArgumentException is thrown if the resulting year is outside the era of the specified DateTime. La partie heure du jour du DateTime résultant reste la même que la DateTimespécifiée.The time-of-day part of the resulting DateTime remains the same as the specified DateTime.

Par exemple, le février compte 28 jours, sauf pendant les années bissextiles, pendant 29 jours.For example, February has 28 days, except during leap years when it has 29 days. Si la date spécifiée est le 29ème jour de février dans une année bissextile et que la valeur de years est 1, la date résultante est le 28ème jour de février de l’année suivante.If the specified date is the 29th day of February in a leap year and the value of years is 1, the resulting date will be the 28th day of February in the following year.

Si years est négatif, le DateTime résultant est antérieur à la DateTimespécifiée.If years is negative, the resulting DateTime is earlier than the specified DateTime.

La propriété Kind de la valeur DateTime retournée est toujours égale à DateTimeKind.Unspecified.The Kind property of the returned DateTime value always equals DateTimeKind.Unspecified. Vous pouvez conserver la propriété Kind du paramètre time en appelant la méthode DateTime.SpecifyKind, comme le montre l’exemple suivant.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)

S’applique à

Voir aussi