tidyverse / lubridate

Make working with dates in R just that little bit easier
https://lubridate.tidyverse.org
GNU General Public License v3.0
728 stars 207 forks source link

Subtracting `today() - months(1)` returns NA in the 31st day of the month. #1079

Closed brendbech closed 1 year ago

brendbech commented 1 year ago

As title says, if you try to subtract one month with months(1) from a date, it will return NA if the date contains the 31st day, i.e on this very day where my program breaks as i have a today() - months(1) variable in a function.

This also occurs when doing as_date('2022-10-31') - months(1). The problem seems to be that subtracting a month from the above date will return a date of 2022-09-31 which is an invalid date, hence the NA.

The workaround is to use days(30) or days(31) i guess, but i'm sure i'm not the first nor the last to hit this error (However, I couldn't find any issue on here reporting it.).

vspinu commented 1 year ago

You need %m+% or add_with_rollaback().