Last date of previous month

Hi All!

How can we get last day of previous month. Example, we are in Jan now. How can we get 2023-12-31 (Dec 31st) ?
Sys.Date() gives current Date. But how can I get last month's date using current date or month?

Thanks for your help!

This seems a little clunky but it gets the answer.

library(lubridate)
LastMonth <- Sys.Date() %m-% months(1)
LastMonth
[1] "2023-12-26"
make_date(year = year(LastMonth), month = month(LastMonth), day = days_in_month(LastMonth))
[1] "2023-12-31"
2 Likes

another approach might be to floor the current month and subtract a day

floor_date(Sys.Date(),unit = "month") %m-% days(1)
3 Likes

This is great!
Thanks @nirgrahamuk!

This topic was automatically closed 7 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.