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 the Statistics Globe Newsletter

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


8 Comments. Leave new

  • Susan George
    March 4, 2021 1:59 pm

    hi thank you for the above,
    I have a quick question, I want to calculate the difference in dates, but both these dates are in the same column.
    So, I have an ID column, visit column- with visit 1 and visit 2 and a date column. So, I want to know the difference in months between visit 1 and visit 2, but not all ID’s have a visit 2.If you can help me figure this.
    Thanks

    Reply
    • Hi Susan,

      Could you provide some example data or a subset of your real data? It’s difficult to calculate without seeing the exact structure of the data.

      You may either post it here or via email to joachim@statisticsglobe.com

      Regards,

      Joachim

      Reply
    • Cast the data to wide, with columns: ID, visit1, visit2. Then visit1 will contain the date of the first visit, and where available, visit2 the date of the second. If there is no second visit, visit2 will be NA. Do the calculation of difference in dates.

      Reply
  • thank you for this code! can you provide code for it to include calculation to 2 or 3 decimal places?

    Reply
    • Hey Liz,

      Thanks for your comment! This is difficult though, because every month has a different number of days.

      You may use the following code to get the number of days between your dates, and then use this difference according to what you want to do with your data.

      interval(date_1, date_2) %/% days(1)

      I hope that helps!

      Joachim

      Reply
  • > date1 date2 interval(start = date1, end = date2 ) %% months(1)
    [1] 2022-06-21 UTC–2022-06-21 UTC Not working here for me

    Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top