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.Max() Method
- C# | Method Parameters
- C# | IsNullOrWhiteSpace() Method
- C# | PadLeft() Method
- C# | Join() Method | Set - 1
- C# | Uri.IsBaseOf(Uri) Method
- C# | Math.Min() Method
- C# | Math.Tan() Method
- Decimal.Add() Method in C#
- C# | String.Contains() Method
- C# | Uri.IsHexDigit() Method
- C# | Math.Exp() Method
- C# | Dictionary.Add() Method
- C# | Math.Abs() Method | Set - 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.