Related Articles

# How to subtract time in R ?

• Last Updated : 28 Apr, 2021

In this article, we will discuss how to subtract time in R Programming Language.

Method 1: Using difftime() method in R

The difftime() method in R is used to compute the difference in the timestamps given. It is used to return an object of the class difftime itself accompanied by units attribute. “difftime” objects support only limited arithmetic operations upon them, that is, they can be added or subtracted, and multiplied or divided by a numeric vector. The result returned by this method is based on the first parameter timestamp value subtracted by the second parameter, that is time1-time2. The result is positive in case the time1 is larger than time2, 0 if the two-time frames are equal and negative for the remaining case.

Syntax: difftime(time1, time2, tz,units = c(“auto”, “secs”, “mins”, “hours”,”days”, “weeks”))

Parameters:

• time1 and time2 – the datetime objects or numeric vectors
• tz – time zone (optional)
• units – the specification of units to perform arithmetic on

Return type: A difftime object applying the arithmetic on datetime object

Example:

## R

 `# declaring first datetime object``time1 <- ``"2019-08-25 17:18:24"`                ` ` `# declaring second datetime object``time2 <- ``"2019-08-30 23:09:24"`  ` ` `# declaring third datetimeobject ``time3 <-  ``"2019-08-22 23:09:24"`` ` `# calculating differences using ``# difftime between time1 and time2``print``(``"TIME1 - TIME2 "``)`` ` `# difference in hours``print ``(``"Difference in hours : "``)``difftime``(time1,time2, units = ``"hours"``)`` ` `# difference in minutes``print ``(``"Difference in minutes : "``)``difftime``(time1,time2,units = ``"mins"``)`` ` `# difference in seconds``print ``(``"Difference in seconds : "``)``difftime``(time1, time2 ,units=``"secs"``)``print``(``"TIME1 - TIME3 "``)`` ` `# calculating differences using difftime``# between time1 and time3``# difference in hours``print ``(``"Difference in hours : "``)``difftime``(time1, time3, units = ``"hours"``)`` ` `# difference in minutes``print ``(``"Difference in minutes : "``)``difftime``(time1, time3, units = ``"mins"``)`` ` `# difference in seconds``print ``(``"Difference in seconds : "``)``difftime``(time1, time3, units=``"secs"``)`

Output:

``` "TIME1 - TIME2 "
 "Difference in hours : "
Time difference of -125.85 hours
 "Difference in minutes : "
Time difference of -7551 mins
 "Difference in seconds : "
Time difference of -453060 secs
 "TIME1 - TIME3 "
 "Difference in hours : "
Time difference of 66.15 hours
 "Difference in minutes : "
Time difference of 3969 mins
 "Difference in seconds : "
Time difference of 238140 secs```

Method 2:

A date string can be first converted to POSIXct objects and then basic arithmetic can be performed on it easily. POSIXct objects ease the process of mathematical operations since they rely on seconds as the major unit of time management. The dates are converted to the standard time zone, UTC. A string type date object can be converted to POSIXct object, using the as.POSIXct(date) method in R. Since, the dates are stored in seconds, the subtraction can be performed by first converting the hours and minutes to the units of seconds too.

```1 hour = 1 * 60 * 60 seconds
1 min = 1 * 60 seconds```

Example 1:

## R

 `# declaring string type date``str_date <- ``"2021-04-01"`` ` `# converting to posixct date``pos_date <- ``as.POSIXct``(str_date)`` ` `# printing original date``print ``(``"Original Date : "``)``print ``(pos_date)`` ` `# subtracting 15 seconds ``date <- pos_date - 15``print ``(``"New Date"``)``print ``(date)`

Output:

``` "Original Date : "
 "2021-04-01 UTC"
 "New Date"
 "2021-03-31 23:59:45 UTC"```

Example 2:

## R

 `# declaring string type date``str_date <- ``"2021-04-01"`` ` `# converting to posixct date``pos_date <- ``as.POSIXct``(str_date)`` ` `# printing original date``print ``(``"Original Date : "``)``print ``(pos_date)`` ` `# subtracting 15 seconds ``secs = 15`` ` `# 25 minutes ``mins = 25 * 60`` ` `# 3 hr ``hrs = 3 * 60 * 60``date <- pos_date - (secs + mins + hrs)``print ``(``"New Date"``)``print ``(date)`

Output:

``` "Original Date : "
 "2021-04-01 UTC"
 "New Date"
 "2021-03-31 20:34:45 UTC"```

Method 3:

Instead, of directly performing arithmetic on the POSIXct dates, we can use the method as.difftime() over this object, to subtract time stamps from the available dates. The as.difftime() method has the following syntax :

`as.difftime(int , units = c("secs","hours","mins")`

Example:

## R

 `# declaring string type date``str_date <- ``"2021-04-01"`` ` `# converting to posixct date``pos_date <- ``as.POSIXct``(str_date)`` ` `# printing original date``print ``(``"Original Date : "``)``print ``(pos_date)`` ` `# subtracting 15 seconds ``secs = 15`` ` `# 25 minutes ``mins = 28`` ` `# 3 hr ``hrs = 8``date <- pos_date - ``as.difftime``(secs,units=``"secs"``) ``- ``as.difftime``(mins,units=``"mins"``) ``- ``as.difftime``(hrs,units=``"hours"``)`` ` `print ``(``"New Date"``)``print ``(date)`

Output

``` "Original Date : "
 "2021-04-01 UTC"
 "New Date"
 "2021-03-31 15:31:45 UTC"```

My Personal Notes arrow_drop_up