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")

Example: Return Number of Months Between Two Dates

install.packages("lubridate")         # Install & load lubridate package
library("lubridate")
interval(my_date_a, my_date_b) %/% # interval & months functions
  months(1)
# 66

The time difference is 66 months.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top