JulianCalendar.AddMonths(DateTime, Int32) Méthode

Définition

Retourne un DateTime qui est le nombre de mois spécifié jusqu’au DateTime spécifié.Returns a DateTime that is the specified number of months away from the specified DateTime.

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

Paramètres

time
DateTime

DateTime auquel ajouter des mois.The DateTime to which to add months.

months
Int32

Nombre de mois à ajouter.The number of months to add.

Retours

DateTime

DateTime qui résulte de l’ajout du nombre de mois spécifié au DateTime spécifié.The DateTime that results from adding the specified number of months 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.

months est inférieur à -120000.months is less than -120000.

-ou--or- months est supérieur à 120000.months is greater than 120000.

Exemples

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

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

*/
using System;
using System.Globalization;

public class SamplesJulianCalendar  {

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

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

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

*/
Imports System.Globalization


Public Class SamplesJulianCalendar   

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

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

'   Era:        1

'   Year:       2002

'   Month:      3

'   DayOfYear:  80

'   DayOfMonth: 21

'   DayOfWeek:  Wednesday

'

'After adding two years and ten months:

'   Era:        1

'   Year:       2005

'   Month:      1

'   DayOfYear:  21

'   DayOfMonth: 21

'   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 année du DateTime résultant est affectée si le mois résultant est en dehors de l’année du DateTimespécifié.The year part of the resulting DateTime is affected if the resulting month is outside the year of 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, si le mois spécifié est octobre, qui contient 31 jours, le jour spécifié est le 31ème jour de ce mois et la valeur du paramètre months est 6, l’année résultante est une année plus grande que l’année spécifiée, le mois résultant est avril et le jour résultant est le 30ème jour, qui est le dernier jour en avril.For example, if the specified month is October, which has 31 days, the specified day is the 31st day of that month, and the value of the months parameter is 6, the resulting year is one more than the specified year, the resulting month is April, and the resulting day is the 30th day, which is the last day in April.

Si la valeur du paramètre months est négative, le DateTime résultant est antérieur à la DateTimespécifiée.If the value of the months parameter 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.AddMonths(time, months), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddMonths(time, months), time.Kind)

S’applique à

Voir aussi