# The difftime R Function | 3 Examples (Return Time Difference in Days, Seconds or Weeks)

**Basic R Syntax:**

difftime(time_1, time_2) |

difftime(time_1, time_2)

The difftime R function **calculates the time difference** of two date or time objects. The basic syntax for difftime in R is shown above.

In the following article, I’ll show you **3 examples** for the usage of difftime in R. Let’s do this!

## Example 1: R difftime – Calculate the Time Difference in Days

As default, the difftime R code calculates the time difference in days. Let’s illustrate that with some example data (i.e. two time characters):

time_1 <- "2019-08-25 19:09:24" # First time object time_2 <- "2019-08-30 23:09:24" # Second time object |

time_1 <- "2019-08-25 19:09:24" # First time object time_2 <- "2019-08-30 23:09:24" # Second time object

Now, we can apply the R difftime command to these two time characters:

difftime(time_1, time_2) # Apply difftime in R # Time difference of -5.166667 days |

difftime(time_1, time_2) # Apply difftime in R # Time difference of -5.166667 days

The RStudio console returns the sentence: *“Time difference of -5.166667 days”*. Easy interpretation: there is a difference between our two time characters of 5.166667 days. The minus sign in front implies that the second time character is at a later point in time than the first time character.

## Example 2: R Convert difftime to Seconds

Let’s assume that for our task we need to return the time difference in seconds. No problem for the difftime R function:

difftime(time_1, time_2, units = "secs") # Apply difftime in seconds # Time difference of -446400 secs |

difftime(time_1, time_2, units = "secs") # Apply difftime in seconds # Time difference of -446400 secs

The time difference of our two dates is 446400 secs. Again, the minus indicates that the second date is after the first date.

## Example 3: Time Difference in Minutes… Hours… Weeks…

Actually, difftime enables the user to calculate the time difference in five different time metrics. Check out the help documentation of the difftime R function:

?difftime # Check help documentation of difftime |

?difftime # Check help documentation of difftime

**Graphic 1: Screenshot of the difftime Help Documentation in R – Highlighting the available Time Metrics.**

As you can see in the highlighted area: difftime provides the time date difference in seconds (as shown in Example 2)…

difftime(time_1, time_2, units = "secs") # Apply difftime in seconds # Time difference of -446400 secs |

…minutes…

difftime(time_1, time_2, units = "mins") # Apply difftime in minutes # Time difference of -7440 mins |

difftime(time_1, time_2, units = "mins") # Apply difftime in minutes # Time difference of -7440 mins

…hours…

difftime(time_1, time_2, units = "hours") # Apply difftime in hours # Time difference of -124 hours |

difftime(time_1, time_2, units = "hours") # Apply difftime in hours # Time difference of -124 hours

…days (the default as shown in Example 1)…

difftime(time_1, time_2, units = "days") # Apply difftime in days # Time difference of -5.166667 days |

difftime(time_1, time_2, units = "days") # Apply difftime in days # Time difference of -5.166667 days

…and weeks:

difftime(time_1, time_2, units = "weeks") # Apply difftime in weeks # Time difference of -0.7380952 weeks |

difftime(time_1, time_2, units = "weeks") # Apply difftime in weeks # Time difference of -0.7380952 weeks

## Handling Dates – Video Instructions

In general, handling dates in R is a special topic. If you are struggling with the conversion or the handling of date data objects, I can recommend the following video tutorial of the YouTuber Vincent Knight:

## Further Reading

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R