lubridate: leap year handling issue

Executing below code on February 29, 2024:

library(lubridate)

from_d <- as.Date(now() - as.period(1, unit = c("year")))

Resulted in a "NA". Which I can understand as 29 February 2023 did not exist. Is this a feature, design of Lubridate, or should I have used another method?

As bypass I modified the code to:
from_d <- as.Date(now() - as.period(52, unit = c("week")))

your bypass is equivalent to

(from_d <- as.Date("2024-02-29")  %m-% weeks(52))

but there is also

(from_d <- as.Date("2024-02-29")  %m-% years(1))
# or # 
(from_d <- as.Date("2024-02-29")  %m-% months(12))
1 Like

hi, thank you very much for providing this solution! I was unfamiliar with the "%m-%" construct.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.