Outputting Difftime as Hh:Mm:Ss:Mm in R

Outputting difftime as HH:MM:SS:mm in R

The subject and content of the post ask for different formats so we will assume that the one in the body of the post is desired, i.e. DD:HH:MM:SS .

Assuming the input, x, must be a vector of seconds or a difftime object:

Fmt <- function(x) UseMethod("Fmt")
Fmt.difftime <- function(x) {
units(x) <- "secs"
x <- unclass(x)
NextMethod()
}
Fmt.default <- function(x) {
y <- abs(x)
sprintf("%s%02d:%02d:%02d:%02d",
ifelse(x < 0, "-", ""), # sign
y %/% 86400, # days
y %% 86400 %/% 3600, # hours
y %% 3600 %/% 60, # minutes
y %% 60 %/% 1) # seconds
}

# test
now <- Sys.time()
now100 <- now + 100

Fmt(now100 - now)
## [1] "00:00:01:40"

Fmt(now - now100)
## "-00:00:01:40"

How to format a difftime object to a string with HH:MM:SS

One possible solution is:

library(hms)

duration <- difftime("2018-07-08 20:08:12 EDT", "2018-07-08 20:07:56 EDT")
as.hms(duration)
# 00:00:16

How to get time differences with output having multiple units

Since you would like days, hours, minutes, seconds, we can get this result with the lubridate package:

a<-c("2018-03-20 11:52:25 AST", "2018-03-20 12:51:25 AST", "2018-03-20 14:19:04 AST",
"2018-03-21 14:12:12 AST", "2018-03-21 12:09:22 AST", "2018-03-21 15:28:01 AST")

b<-c("2018-04-09 18:39:38 AST", "2018-06-23 19:13:14 AST", "2018-03-20 23:23:03 AST",
"2018-05-10 21:29:28 AST", "2018-03-22 03:17:23 AST", "2018-05-12 00:19:39 AST")

a = as.POSIXct(a)
b = as.POSIXct(b)

library(lubridate)
timespan = interval(ymd_hms(ab[,1]), ymd_hms(ab[,2]))
> as.period(timespan)
[1] "20d 6H 47M 13S" "3m 3d 6H 21M 49S" "9H 3M 59S" "1m 19d 7H 17M 16S"
[5] "15H 8M 1S" "1m 20d 8H 51M 38S"

If desired, we can convert months to days by specifying the formatting as follows:

> as.period(timespan, unit = "day")
[1] "20d 6H 47M 13S" "95d 6H 21M 49S" "9H 3M 59S" "50d 7H 17M 16S"
[5] "15H 8M 1S" "51d 8H 51M 38S"

How to format difftime as hh:mm in ggplot2?

The answer has two parts.

Plotting difftime objects

According to help("scale_x_time"), ggplot2 supports three date/time classes: scale_*_date for dates (class Date), scale_*_datetime for datetimes (class POSIXct), and scale_*_time for times (class hms). The last one is what we need here.

Class hms is a custom class for difftime vectors. as.hms() has a method for difftime. So. difftime objects can be plotted with ggplot2 by coercing to class hms:

a <- as.difftime(c(-60, -4 * 60),  unit = "mins")
b <- as.difftime(c(-60, 2 * 60 + 47), unit = "mins")
library(ggplot2)
ggplot(data = NULL, aes(x = hms::as.hms(b), y = hms::as.hms(a))) +
geom_point(shape = 1)

Sample Image

Please, note that negative time differences are shown as well.

Formatting the tick labels

The OP has requested that tick marks should be labeled in hh:mm format. Apparently, the default formatting is hh:mm:ss. This can be modified by specifying a function that takes the breaks as input and returns labels as output to the labels parameter of the scale_x_time() and scale_y_time() functions:

format_hm <- function(sec) stringr::str_sub(format(sec), end = -4L)
ggplot(data = NULL, aes(x = hms::as.hms(b), y = hms::as.hms(a))) +
geom_point(shape = 1) +
scale_x_time(name = "b", labels = format_hm) +
scale_y_time(name = "a", labels = format_hm)

Sample Image

The format_hm() function truncates the :ss part from the default format. In addition, the axis are labeled nicely.

Calculate time difference in %H:%M:%S format in R

You could use hms library.

require(hms)
x <- data.frame(TIME_SPENT = as.difftime(runif(10) * 10, format = 'H', units = 'hours'))
x$TIME_SPENT_HMS <- hms(hours = as.numeric(x$TIME_SPENT))

> x
TIME_SPENT TIME_SPENT_HMS
1 0.670335 hours 00:40:13.206023
2 6.740590 hours 06:44:26.122495
3 9.433242 hours 09:25:59.670743
4 9.350243 hours 09:21:00.873019
5 1.459504 hours 01:27:34.214544
6 4.820711 hours 04:49:14.560171
7 5.052186 hours 05:03:07.870653
8 9.415136 hours 09:24:54.489527
9 4.717802 hours 04:43:04.086949
10 4.131969 hours 04:07:55.087836

It supports objects of class difftime (but units are always seconds) and stores the values in HH:MM:SS format.

> class(x$TIME_SPENT_HMS)
[1] "hms" "difftime"

You can perform all sort of calculations on it.

> x$TIME_SPENT_HMS[1] + 10
Time difference of 2423.206 secs

Values displayed in table view are also represented with semicolons:

Sample Image

R - How to convert decimal to MM:SS.XXX

one option is the lubridate package - since you did not specify the output class I included a few possible outputs:

package(lubridate)

t <- 92.180
# your output string as character
lubridate::seconds(t) %>%
lubridate::as_datetime() %>%
format("%M:%OS3")

# output as period
lubridate::seconds(t) %>%
lubridate::as.period()

# output as duration
lubridate::seconds(t) %>%
lubridate::as.duration()

# output as time time
lubridate::seconds(t) %>%
lubridate::as.difftime()

R Find time difference in seconds for YYYY-MM-DD HH:MM:SS.MMM format

Easy peasy:

R> now <- Sys.time()
R> then <- Sys.time()
R> then - now
Time difference of 5.357 secs
R> class(then - now)
[1] "difftime"
R> as.numeric(then - now)
[1] 5.357
R>

And for your data:

R> df
time2 time3
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752
2 2011-09-05 12:25:37.42 2011-09-05 12:25:01.187
3 2011-08-24 12:56:58.91 2011-08-24 12:55:13.012
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759
R> df$time2 <- strptime(df$time2, "%Y-%m-%d %H:%M:%OS")
R> df$time3 <- strptime(df$time3, "%Y-%m-%d %H:%M:%OS")
R> df
time2 time3
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752
2 2011-09-05 12:25:37.420 2011-09-05 12:25:01.187
3 2011-08-24 12:56:58.910 2011-08-24 12:55:13.012
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759
R> df$time2 - df$time3
Time differences in secs
[1] 52.781 36.233 105.898 82.963 179.938
attr(,"tzone")
[1] ""
R>

and added back as a numeric to the data frame:

R> df$dt <- as.numeric(df$time2 - df$time3)
R> df
time2 time3 dt
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752 52.781
2 2011-09-05 12:25:37.420 2011-09-05 12:25:01.187 36.233
3 2011-08-24 12:56:58.910 2011-08-24 12:55:13.012 105.898
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759 82.963
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759 179.938
R>

Convert long time difference to format HH:mm [Java]

Once easy thing you can do is call getTime() for both dates and then subtract them like so:

long timeDiff = today.getTime() - ts1.getTime()

That should give you the difference in miliseconds between the two times. After that you know that one second is 1k miliseconds, 1min i 60s, 1h is 60 minutes and so on.

How to convert decimal time to time format

Another option:

library(lubridate)
video$video_duration <- as.numeric(video_end - video_begin, units = "secs")
video$video_duration <- seconds_to_period(video$video_duration)

video_begin video_end video_duration
1 2017-09-12 00:08:14 2017-09-12 00:39:08 30M 54S
2 2017-09-12 00:04:47 2017-09-12 00:47:10 42M 23S
3 2017-09-12 00:08:27 2017-09-12 00:49:51 41M 24S
4 2017-09-12 00:04:59 2017-09-12 00:44:31 39M 32S
5 2017-09-12 00:04:57 2017-09-12 00:39:41 34M 44S
6 2017-09-12 00:07:51 2017-09-12 00:47:12 39M 21S
7 2017-09-12 00:06:11 2017-09-12 00:40:13 34M 2S
8 2017-09-12 00:05:30 2017-09-12 00:46:52 41M 22S


Related Topics



Leave a reply



Submit