Number of Months Between Two Dates in R (Example)


In this tutorial, I’ll show how to compute the number of months between two different date objects in the R programming language.

The article will consist of one example for the counting of months. To be more precise, the tutorial consists of this content:

You’re here for the answer, so let’s get straight to the example.


Example Data

We’ll use the following data as basement for this R programming tutorial.

date_1 <- as.Date("2020-08-10")           # Create example dates
date_2 <- as.Date("2025-01-01")

Have a look at the previous R code. It shows that our example data are two date objects.


Example: Computing Month Difference Using lubridate Package

The following code illustrates how to count the number of months between two dates based on the interval and months functions of the lubridate package.

First, we have to install and load the lubridate add-on package:

install.packages("lubridate")             # Install lubridate package
library("lubridate")                      # Load lubridate package

Now, we can apply the interval and months to our two date objects as shown below:

interval(date_1, date_2) %/% months(1)    # Apply interval & months
# 52

As you can see based on the previously shown output of the RStudio console, the time difference between our two dates is 52 months.


Video & Further Resources

Do you need further information on the R programming codes of this article? Then you could have a look at the following video of my YouTube channel. In the video, I show the R programming code of this tutorial.


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


In addition to the video, you might have a look at the related posts on my website. You can find a selection of tutorials about the handling of dates below.


Summary: In this post, I explained how to get the number of months between multiple dates in the R programming language. Let me know in the comments section, if you have further questions.


Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

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.