Subtract hours from time in R
Last Updated :
23 Jul, 2025
The time objects in R programming language 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.
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 the as.POSIXct(date) method in R. Since, the dates are stored on 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
Syntax:
as.POSIXct ( date , format)
The result is returned as a datetime object with the respective number of hours subtracted belonging to the POSIXct class.
Example:
R
# declaring a time object
time1 <- as.POSIXct("08:32:07", format = "%H:%M:%S")
print ("Time")
print (time1)
# converting 3 hours to seconds
hrs <- 3 * 60 * 60
print ("Subtracted Time")
print (time1 - hrs)
Output
[1] "Time"
[1] "2021-05-19 08:32:07 UTC"
[1] "Subtracted Time"
[1] "2021-05-19 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.
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 integer denoting the number of hours. The lubridate package objects allow direct arithmetic over their various components, therefore the number of hours can be directly subtracted from the lubricate time object. A result is also an object belonging to this class.
Example:
R
# getting required libraries
library(lubridate)
# getting current time
time <- Sys.time()
print("Current time")
print (time)
# subtracting hours
hrs <- hours(5)
mod_time <- time - hrs
print ("Subtracting 5 hours")
print (mod_time)
Output
[1] "Current time"
[1] "2021-05-19 16:31:37 IST"
[1] "Subtracting 5 hours"
[1] "2021-05-19 11:31:37 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 = "")
Parameter :
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. Also, the date is returned which is essential in the cases, where the subtraction of certain hours results in the change of date too.
Example:
R
# declaring a time object
time1 <- strptime("00:32:07", format = "%H:%M:%S")
print ("Time")
print (time1)
# converting 5 hours to seconds
hrs <- 5 * 60 * 60
print ("Subtracted Time")
print (time1 - hrs)
Output
[1] "Time"
[1] "2021-05-19 00:32:07 IST"
[1] "Subtracted Time"
[1] "2021-05-18 19:32:07 IST"
Similar Reads
How to subtract time in R ? 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. "d
4 min read
Get the Hour from timestamp in Pandas Let's learn how to get the hour from the timestamp in Pandas DataFrame. Extract Hour from Timestamps in Pandas DataFramePandas offers convenient tools to extract specific time components from timestamps in your data, such as the hour, which is useful for time-based analysis.Convert the timestamp to
3 min read
Get the Hour from timestamp in Pandas Let's learn how to get the hour from the timestamp in Pandas DataFrame. Extract Hour from Timestamps in Pandas DataFramePandas offers convenient tools to extract specific time components from timestamps in your data, such as the hour, which is useful for time-based analysis.Convert the timestamp to
3 min read
How to Add and Subtract Days to and from Date in R? Managing dates and times is a crucial aspect of data analysis, and R provides robust facilities for dealing with date objects. In this post, we will look at how to add and remove days from a date in R. Whether you're dealing with time series data, need to compute future dates, or want to browse thro
4 min read
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
4 min read
How to compare time in R? R programming Language supports both date and DateTime objects using various different formats and specifiers. The built-in framework as.Date function is responsible for the handling of dates alone, the library chron in R Programming handles both dates and times, without any support for time zones;
4 min read