To add/subtract time, use POSIXct, since it stores times in seconds
## adding/subtracting times - 60 seconds
as.POSIXct("2016-01-01") + 60
# [1] "2016-01-01 00:01:00 AEDT"
## adding 3 hours, 14 minutes, 15 seconds
as.POSIXct("2016-01-01") + ( (3 * 60 * 60) + (14 * 60) + 15)
# [1] "2016-01-01 03:14:15 AEDT"
More formally, as.difftime
can be used to specify time periods to add to a date or datetime object. E.g.:
as.POSIXct("2016-01-01") +
as.difftime(3, units="hours") +
as.difftime(14, units="mins") +
as.difftime(15, units="secs")
# [1] "2016-01-01 03:14:15 AEDT"
To find the difference between dates/times use difftime()
for differences in seconds, minutes, hours, days or weeks.
# using POSIXct objects
difftime(
as.POSIXct("2016-01-01 12:00:00"),
as.POSIXct("2016-01-01 11:59:59"),
unit = "secs")
# Time difference of 1 secs
To generate sequences of date-times use seq.POSIXt()
or simply seq
.