This method is used to return a new DateTime that adds the specified number of months to the value of this instance.
public DateTime AddMonths (int months);
Here, months is the number of months. The months parameter can be negative or positive.
Return Value: This method returns an object whose value is the sum of the date and time represented by this instance and months.
Exception: This method will throw ArgumentOutOfRangeException if the resulting DateTime is less than MinValue or greater than MaxValue
or months is less than -120, 000 or greater than 120, 000.
Below programs illustrate the use of the above-discussed method:
04/17/2018 05/17/2018 06/17/2018 07/17/2018 08/17/2018 09/17/2018 10/17/2018 11/17/2018 12/17/2018 01/17/2019 02/17/2019 In Leap Years: 04/30/2016
System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime.
Parameter name: months
- This method does not change the value of this DateTime object. Instead, it returns a new DateTime object whose value is the result of this operation.
- This calculates the resulting month and year, taking into account leap years and the number of days in a month, then adjusts the day part of the resulting DateTime object.
- The time-of-day part of the resulting DateTime object remains the same as this instance.
- Difference between Method Overriding and Method Hiding in C#
- C# | Math.Pow() Method
- C# | CopyTo() Method
- C# | Clone() Method
- Queue.Contains() Method in C#
- Stack.Pop() Method in C#
- C# | Join Method | Set - 2
- Stack.Contains() Method in C#
- Method Hiding in C#
- C# | Trim() Method
- C# | EndsWith() Method
- C# | Math.Log() Method
- Main Method in C#
- TimeSpan.Add() Method in C#
- C# | Uri.IsHexEncoding() Method
- DateTimeOffset.Add() Method in C#
- Anonymous Method in C#
- C# | PadRight() Method
- C# | String.Contains() Method
- C# | Math.Sin() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.