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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.