Click or drag to resize

DayPlusMonths Method

Returns the Day that comes the specified numberOfMonths after this Day in the calendar.

Namespace: DegreeDays.Time
Assembly: DegreeDays (in DegreeDays.dll) Version: 1.4
Syntax
public Day PlusMonths(
	int numberOfMonths
)

Parameters

numberOfMonths  Int32
The number of months to add to this Day. Can be positive (to go forwards in time), zero, or negative (to go backwards in time).

Return Value

Day
A Day representing the result of the operation.
Exceptions
ExceptionCondition
OverflowExceptionIn the unlikely event of this method requiring a Day with a year less than 1 or greater than 9999.
Remarks

This will happily cross the borders between years, and it will always ensure that the returned result is a valid Day, unless it is outside the range allowed by Day (0001-01-01 to 9999-12-31 inclusive), in which case it will throw an OverflowException.

The DayOfMonth of the returned Day will be the same as that of this Day when possible, but sometimes it will have to be reduced, to fit within the constraints of the resulting month. For example:

  • 2020-05-31 plus 1 month would give a result of 2020-06-30 (because June only has 30 days, 2020-06-31 would not be a valid date, so this will return 2020-06-30 instead).
  • 2019-12-31 plus 2 months would give a result of 2020-02-29 (the last day in February 2020, which has 29 days instead of 28 because 2020 is a leap year).

However, the DayOfMonth will only change when absolutely necessary, for example:

  • 2020-01-31 plus 2 months will give a result of 2020-03-31. It does not matter that February 2020 had only 29 days because we are adding 2 months, not 1.
  • 2016-02-29 plus 48 months (i.e. 4 years) will give a result of 2020-02-29. 2016 and 2020 are both leap years, it does not matter that the years in between are not.

Day is immutable, so calling this method will not change this Day.

See Also