Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Calendar.AddMonths Method

When overridden in a derived class, returns a DateTime that is the specified number of months away from the specified DateTime.

[Visual Basic]
Public MustOverride Function AddMonths( _
   ByVal time As DateTime, _
   ByVal months As Integer _
) As DateTime
[C#]
public abstract DateTime AddMonths(
 DateTime time,
 int months
);
[C++]
public: virtual DateTime AddMonths(
 DateTime time,
 int months
) = 0;
[JScript]
public abstract function AddMonths(
   time : DateTime,
 months : int
) : DateTime;

Parameters

time
The DateTime to which to add months.
months
The number of months to add.

Return Value

The DateTime that results from adding the specified number of months to the specified DateTime.

Exceptions

Exception Type Condition
ArgumentException The resulting DateTime is outside the supported range.

Remarks

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. The year part of the resulting DateTime is affected if the resulting month is outside the year of the specified DateTime. The era part of the resulting DateTime is affected if the resulting year is outside the era of the specified DateTime. The time-of-day part of the resulting DateTime remains the same as the specified DateTime.

For example, suppose that the current calendar has 12 months where the fourth month has 30 days and the tenth month has 31 days. If the specified month is the tenth month, 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 the fourth month, and the resulting day is the 30th day, which is the last day of the resulting month.

If the value of the months parameter is negative, the resulting DateTime is earlier than the specified DateTime.

Example

[Visual Basic, C#, C++] The following code example demonstrates the members of the Calendar class.

[Visual Basic] 
Imports System
Imports System.Globalization


Public Class SamplesCalendar   

   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())

      ' Uses the default calendar of the InvariantCulture.
      Dim myCal As Calendar = CultureInfo.InvariantCulture.Calendar

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

      ' Adds 5 to every component of the DateTime.
      myDT = myCal.AddYears(myDT, 5)
      myDT = myCal.AddMonths(myDT, 5)
      myDT = myCal.AddWeeks(myDT, 5)
      myDT = myCal.AddDays(myDT, 5)
      myDT = myCal.AddHours(myDT, 5)
      myDT = myCal.AddMinutes(myDT, 5)
      myDT = myCal.AddSeconds(myDT, 5)
      myDT = myCal.AddMilliseconds(myDT, 5)

      ' Displays the values of the DateTime.
      Console.WriteLine("After adding 5 to each component of the DateTime:")
      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("   Hour:         {0}", myCal.GetHour(myDT))
      Console.WriteLine("   Minute:       {0}", myCal.GetMinute(myDT))
      Console.WriteLine("   Second:       {0}", myCal.GetSecond(myDT))
      Console.WriteLine("   Milliseconds: {0}", myCal.GetMilliseconds(myDT))
      Console.WriteLine()
   End Sub 'DisplayValues

End Class 'SamplesCalendar 


'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

'   Hour:         0

'   Minute:       0

'   Second:       0

'   Milliseconds: 0

'

'After adding 5 to each component of the DateTime:

'   Era:          1

'   Year:         2007

'   Month:        10

'   DayOfYear:    286

'   DayOfMonth:   13

'   DayOfWeek:    Saturday

'   Hour:         5

'   Minute:       5

'   Second:       5

'   Milliseconds: 5

[C#] 
using System;
using System.Globalization;


public class SamplesCalendar  {

   public static void Main()  {

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

      // Uses the default calendar of the InvariantCulture.
      Calendar myCal = CultureInfo.InvariantCulture.Calendar;

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

      // Adds 5 to every component of the DateTime.
      myDT = myCal.AddYears( myDT, 5 );
      myDT = myCal.AddMonths( myDT, 5 );
      myDT = myCal.AddWeeks( myDT, 5 );
      myDT = myCal.AddDays( myDT, 5 );
      myDT = myCal.AddHours( myDT, 5 );
      myDT = myCal.AddMinutes( myDT, 5 );
      myDT = myCal.AddSeconds( myDT, 5 );
      myDT = myCal.AddMilliseconds( myDT, 5 );

      // Displays the values of the DateTime.
      Console.WriteLine( "After adding 5 to each component of the DateTime:" );
      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( "   Hour:         {0}", myCal.GetHour( myDT ) );
      Console.WriteLine( "   Minute:       {0}", myCal.GetMinute( myDT ) );
      Console.WriteLine( "   Second:       {0}", myCal.GetSecond( myDT ) );
      Console.WriteLine( "   Milliseconds: {0}", myCal.GetMilliseconds( 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
   Hour:         0
   Minute:       0
   Second:       0
   Milliseconds: 0

After adding 5 to each component of the DateTime:
   Era:          1
   Year:         2007
   Month:        10
   DayOfYear:    286
   DayOfMonth:   13
   DayOfWeek:    Saturday
   Hour:         5
   Minute:       5
   Second:       5
   Milliseconds: 5

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

void DisplayValues(Calendar* myCal, DateTime myDT) {
   Console::WriteLine(S"   Era: {0}", __box(myCal->GetEra(myDT)));
   Console::WriteLine(S"   Year: {0}", __box(myCal->GetYear(myDT)));
   Console::WriteLine(S"   Month: {0}", __box(myCal->GetMonth(myDT)));
   Console::WriteLine(S"   DayOfYear: {0}", __box(myCal->GetDayOfYear(myDT)));
   Console::WriteLine(S"   DayOfMonth: {0}", __box(myCal->GetDayOfMonth(myDT)));
   Console::WriteLine(S"   DayOfWeek: {0}", __box(myCal->GetDayOfWeek(myDT)));
   Console::WriteLine(S"   Hour: {0}", __box(myCal->GetHour(myDT)));
   Console::WriteLine(S"   Minute: {0}", __box(myCal->GetMinute(myDT)));
   Console::WriteLine(S"   Second: {0}", __box(myCal->GetSecond(myDT)));
   Console::WriteLine(S"   Milliseconds: {0}", __box(myCal->GetMilliseconds(myDT)));
   Console::WriteLine();
}

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

   // Uses the default calendar of the InvariantCulture.
   Calendar*  myCal = CultureInfo::InvariantCulture->Calendar;

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

   // Adds 5 to every component of the DateTime.
   myDT = myCal->AddYears(myDT, 5);
   myDT = myCal->AddMonths(myDT, 5);
   myDT = myCal->AddWeeks(myDT, 5);
   myDT = myCal->AddDays(myDT, 5);
   myDT = myCal->AddHours(myDT, 5);
   myDT = myCal->AddMinutes(myDT, 5);
   myDT = myCal->AddSeconds(myDT, 5);
   myDT = myCal->AddMilliseconds(myDT, 5);

   // Displays the values of the DateTime.
   Console::WriteLine(S"After adding 5 to each component of the DateTime:");
   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
Hour:         0
Minute:       0
Second:       0
Milliseconds: 0

After adding 5 to each component of the DateTime:
Era:          1
Year:         2007
Month:        10
DayOfYear:    286
DayOfMonth:   13
DayOfWeek:    Saturday
Hour:         5
Minute:       5
Second:       5
Milliseconds: 5

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Calendar Class | Calendar Members | System.Globalization Namespace | System.DateTime | AddYears | AddWeeks | AddDays | AddHours | AddMinutes | AddSeconds | AddMilliseconds

Show:
© 2014 Microsoft. All rights reserved.