0

I currently use the difftime() function to get the elapsed time between two Sys.time() objects.

ie :

t1 <- Sys.time()
t2 <- Sys.time()
elapsed_time <- difftime(t2, t1)

The current output is something like :

5.188578 days

I would like to get an output that show the elapsed time this way :

5 days : 4 hours : 30 mins : 15 secs

or something similar to this.

I already tried the strptime() function, but it seems to work for dates so I'm not sure how I could use it.

I also looked at the lubridate periods() functions that seems to give output like this : "3m 12d 0H 0M 0S" (which would be great) but it doesn't seems to work with my elapsed_time object.

I also looked on stackoverflow and google but didn't find the answer I wanted.

so I was wondering if anyone could give me some clues on how to proceed. Thank you.

Edit : I found a somewhat good answer With Dave's comment and How to get time differences with output having multiple units page.

library(lubridate)
t1 <- Sys.time()
t2 <- Sys.time()
elapsed_time <- difftime(t2, t1)

as.period(elapsed_time)

Output result format is :

"3H 37M 55S"
Gainz
  • 1,721
  • 9
  • 24
  • 1
    Try this answer: https://stackoverflow.com/questions/55328410/how-to-get-time-differences-with-output-having-multiple-units/55328744#55328744 – Dave2e May 13 '19 at 15:36
  • 1
    Thanks Dave, seems like I just had to do as.period(elapsed_time) to make it work. Not sure why it didn't work when I tried it, I probably did a mistake. The output is not 100% what I want but it is still good enough. Thank you. – Gainz May 13 '19 at 15:51

0 Answers0