How to Add or subtract time span to a datetime in R ?
The time objects in R can be declared either using POSIXct class, which offers fast manipulation and storage of such objects. External packages in R also help in working with time and dates and allow both comparison and direct arithmetic operations to be performed upon them. In this article, we are going to see how to add and subtract a time span to a DateTime in R Programming.
Method 1: Using POSIXct object
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 them as.POSIXct(date) method in R. Since, the dates are stored in terms of seconds, the subtraction, as well as addition, can be performed by first converting the hours and minutes to the units of seconds too. Mathematical operators can directly be used to add various time components to the date object, which belongs to the POSIXct class.
1 hour = 1 * 60 * 60 seconds 1 min = 1 * 60 seconds
Syntax: as.POSIXct ( date , format)
date – The string date object
format – The format specifier of the date
 "Original DateTime"  "2021-05-08 08:32:07 UTC"  "Adding 20 mins to DateTime"  "2021-05-08 08:52:07 UTC"  "Subtracting 3 hours from DateTime"  "2021-05-08 05:32:07 UTC"
Method 2: Using lubridate package
Lubridate package in R is used to work with date and time objects. It makes it easier to parse and manipulate the objects and needs to be installed and loaded into the working space by the following command :
The Sys.time() function in R is used to fetch the current date and time object according to the IST zone. The hours() method in R is used to take an input of an integer denoting the number of hours. The “lubridate” package objects allow direct arithmetic over its various components, therefore the number of hours can be directly subtracted from the lubridate time object. A result is also an object belonging to this class.
 "Current time"  "2021-05-22 03:27:02 IST"  "Subtracting 5 hours"  "2021-05-21 22:27:02 IST"  "Adding 17 seconds"  "2021-05-22 03:27:19 IST"
Method 3: Using strptime() method
strptime method in R is used to directly convert character vectors (of a variety of formats) to POSIXlt format. strptime is faster than the previous approach, because strptime only handles character input.
Syntax: strptime(date, format, tz = “”)
date – The date in character format
format – The format specifier of the input date
tz – time zone (optional)
strptime() works similar to the POSIXct objects, where all the calculations are done in terms of seconds.
 "Time"  "2021-07-07 00:32:07 UTC"  "Subtracting 5 hours"  "2021-07-06 19:32:07 UTC"  "Modified Time"  "2021-07-05 00:56:07 UTC"
Explanation: The subtraction of 5 hours, leads to the return of the previous date. In the second scenario, the respective number of minutes and seconds are computed.