Time Difference Between Dates in Weeks, Days, Hours, Minutes & Seconds in R (5 Examples)
In this article, I’ll illustrate how to return the time interval between two Date objects in the R programming language.
The tutorial will consist of five examples for the computation of time differences. To be more specific, the content of the article is structured as follows:
Let’s do this:
We’ll use the following data as basement for this R tutorial:
date1 <- as.Date("2023-05-07", format = "%Y-%m-%d") # Create first date date2 <- as.Date("2020-11-15", format = "%Y-%m-%d") # Create second date
Example 1: Get Time Difference Between Two Dates in Weeks
In Example 1, I’ll explain how to get the time interval between our two example dates in weeks. For this, we can use the difftime function as shown below:
difftime(date1, date2, units = "weeks") # Time interval in weeks # Time difference of 129 weeks
The RStudio console output shows the time difference between our two dates, i.e. 129 weeks.
Example 2: Get Time Difference Between Two Dates in Days
Example 2 explains how to return the time difference in days by specifying the units argument within the difftime function to be equal to “days”:
difftime(date1, date2, units = "days") # Time interval in days # Time difference of 903 days
The first and the second date are 903 days away from each other.
Example 3: Get Time Difference Between Two Dates in Hours
In this example, I’ll show how to compute the hours between two dates:
difftime(date1, date2, units = "hours") # Time interval in hours # Time difference of 21672 hours
As you can see based on the previous output of the RStudio console, our two example dates have a difference of 21672 hours.
Example 4: Get Time Difference Between Two Dates in Minutes
In Example 4, I’ll explain how to get the time interval between two Date objects in minutes.
difftime(date1, date2, units = "mins") # Time interval in minutes # Time difference of 1300320 mins
The first date is 1300320 minutes later than the second date.
Example 5: Get Time Difference Between Two Dates in Seconds
In this example, I’ll explain how to show the time difference between two dates in seconds:
difftime(date1, date2, units = "secs") # Time interval in seconds # Time difference of 78019200 secs
The difftime function returns the result: The second date is 78019200 seconds earlier as the first date.
Video, Further Resources & Summary
Have a look at the following video of my YouTube channel. I’m explaining the R codes of this tutorial in the video:
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.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you may want to read some of the other posts on how to count weeks, days, hours, minutes, and seconds between two points in time.
You learned in this tutorial how to calculate time differences in different metrics in the R programming language.
Note that this tutorial has explained how to compare two dates and how to get the time difference of two dates. However, you may apply the same R code to vectors of dates or two columns in a data frame. You may even create a new data frame variable that contains the time difference between two columns.
In case you have further questions about this or any other topic, don’t hesitate to let me know in the comments section below. Furthermore, don’t forget to subscribe to my email newsletter in order to receive updates on the newest tutorials.
Statistics Globe Newsletter