DescTools (version 0.99.8.1)

AddMonths: Add a Month to a Date

Description

Add a number of months to a date. In case the number is negative, the months will be subtracted. A ceiling to the last available day of the month can be set. The function will then yield as.Date('2013-01-31') + 1 month = '2013-02-28'.

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)

Run the code above in your browser using DataLab