R Time Difference: Seconds, Minutes, Hours, Days & Weeks (5 Examples)

In this R article you’ll learn how to return the time interval between two Date objects.

Creation of Example Data

first_date <- as.Date("2025-01-17",
                      format = "%Y-%m-%d")     # Create first date
second_date <- as.Date("2018-12-26",
                       format = "%Y-%m-%d")    # Create second date

Example 1: Return Time Interval Between Two Date Objects in Seconds

difftime(first_date,                           # Time difference in seconds
         second_date,
         units = "secs")
# Time difference of 191289600 secs

Example 2: Return Time Interval Between Two Date Objects in Minutes

difftime(first_date,                           # Time difference in minutes
         second_date,
         units = "mins")
# Time difference of 3188160 mins

Example 3: Return Time Interval Between Two Date Objects in Hours

difftime(first_date,                           # Time difference in hours
         second_date,
         units = "hours")
# Time difference of 53136 hours

Example 4: Return Time Interval Between Two Date Objects in Days

difftime(first_date,                           # Time difference in days
         second_date,
         units = "days")
# Time difference of 2214 days

Example 5: Return Time Interval Between Two Date Objects in Weeks

difftime(first_date,                           # Time difference in weeks
         second_date,
         units = "weeks")
# Time difference of 316.2857 weeks

Leave a Reply

Your email address will not be published.

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