%m+%
From lubridate v1.2.0
by Garrett Grolemund
Add and subtract months to a date without exceeding the last day of the new month
Adding months frustrates basic arithmetic because consecutive months have different lengths. With other elements, it is helpful for arithmetic to perform automatic roll over. For example, 12:00:00 + 61 seconds becomes 12:01:01. However, people often prefer that this behavior NOT occur with months. For example, we sometimes want January 31 + 1 month = February 28 and not March 3. months(n) always returns a date in the nth month after Date. If the new date would usually spill over into the n + 1th month, month. Date nth month before Date.
Usage
e1 %m+% e2
Arguments
Details
These must be added separately with traditional arithmetic. they are not a one-to-one operations and results for either will be sensitive to the order of operations.
Value
- A date-time object of class POSIXlt, POSIXct or Date
Examples
jan <- ymd_hms("2010-01-31 03:04:05")
# "2010-01-31 03:04:05 UTC"
jan + months(1:3) # Feb 31 and April 31 prompt "rollover"
# "2010-03-03 03:04:05 UTC" "2010-03-31 03:04:05 UTC" "2010-05-01 03:04:05 UTC"
jan %m+% months(1:3) # No rollover
# "2010-02-28 03:04:05 UTC" "2010-03-31 03:04:05 UTC" "2010-04-30 03:04:05 UTC"
leap <- ymd("2012-02-29")
"2012-02-29 UTC"
leap %m+% years(1)
# "2013-02-28 UTC"
leap %m+% years(-1)
leap %m-% years(1)
# "2011-02-28 UTC"
Community examples
Looks like there are no examples yet.