DescTools (version 0.99.14)

AddMonths: Add a Month to a Date

Description

Simple adding a number of months to a date can lead to invalid dates, think of e.g. 2012-01-30 + 1 month. This function offers a ceiling option to make sure that the result is always a valid date, e.g. as.Date("2013-01-31") + 1 month will be "2013-02-28". If number n is negative, the months will be subtracted.

Usage

AddMonths(x, n, ceiling = TRUE)

Arguments

x
the date to which a number of months has to be added. x can be a date or an integer. If it is an integer it will be interpreted as yyyymm.
n
the number of months to be added. If n is negative the months will be subtracted.
ceiling
logic. If set to TRUE (default), a ceiling to the last available day of month will be set.

Value

  • a vector with the same dimension and type as x, containing the transformed dates.

References

Thanks to Antonio: http://stackoverflow.com/questions/14169620/add-a-month-to-a-date

See Also

Date functions, like Year, Month, etc.

Examples

Run this code
AddMonths(as.Date("2013-01-01"), 10)
AddMonths(as.Date("2013-01-01"), -5)

AddMonths(201301, 3)
AddMonths(201301, -5)

AddMonths(as.Date("2013-01-31"), 1)
AddMonths(as.Date("2013-01-31"), -2)

AddMonths(as.Date(c("2014-10-12","2013-01-31","2011-12-05")), 3)

Run the code above in your browser using DataCamp Workspace