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:

 

Example Data

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

The previous RStudio console output shows that our example data objects are two dates. These data objects were converted to the Date class using the as.Date function.

 

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:

 

 

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.

 

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.


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.

Top