How to Get Number of Months Between 2 Dates in R (Example Code)
In this R tutorial you’ll learn how to get the number of months between multiple dates.
Example Data
my_date_a <- as.Date("2021-10-10") # Storing some example dates my_date_b <- as.Date("2027-05-05") |
my_date_a <- as.Date("2021-10-10") # Storing some example dates my_date_b <- as.Date("2027-05-05")
Example: Return Number of Months Between Two Dates
install.packages("lubridate") # Install & load lubridate package library("lubridate") |
install.packages("lubridate") # Install & load lubridate package library("lubridate")
interval(my_date_a, my_date_b) %/% # interval & months functions months(1) # 66 |
interval(my_date_a, my_date_b) %/% # interval & months functions months(1) # 66
The time difference is 66 months.