Home > Software engineering >  strict difference between two dates in R lubridate
strict difference between two dates in R lubridate

Time:10-14

I have the following code using lubridate in R:

interval("2022-01-20","2022-02-01") %/% months(1)

answer:0

But the answer I'm looking for is "1" instead of 0

CodePudding user response:

ceiling(interval("2022-01-20","2022-02-01") / months(1))

CodePudding user response:

If we want to get 1 month difference, we may need to floor, otherwise it is just 12 days and it is still less than a month

library(lubridate)
interval(floor_date(as.Date("2022-01-20"), 
        "month"),as.Date("2022-02-01")) %/% months(1)
[1] 1
  •  Tags:  
  • r
  • Related