Calendar.AddMonths Method

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

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

abstract AddMonths : 
        time:DateTime * 
        months:int -> DateTime

Parameters

time
Type: System.DateTime

The DateTime to which to add months.

months
Type: System.Int32

The number of months to add.

Return Value

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

ExceptionCondition
ArgumentException

The resulting DateTime is outside the supported range of this calendar.

ArgumentOutOfRangeException

months is outside the supported range of the DateTime return value.

The months value is added to the specified DateTime. If months is negative, the resulting DateTime is earlier than the specified DateTime.

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.

The Kind property of the returned DateTime value always equals DateTimeKind.Unspecified. You can preserve the Kind property of the time parameter by calling the DateTime.SpecifyKind method, as the following example shows.

No code example is currently available or this language may not be supported.

The following code example demonstrates the members of the Calendar class.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft