Home > Enterprise >  How to change format from seconds to hh:mm:ss?
How to change format from seconds to hh:mm:ss?

Time:10-05

I need to transform a seconds to hh:mm:ss format in R. I use the difftime() function to have the difference between two dates and I specify units="mins".

But for the final result, I want it as hh:mm:ss and not only by specifying hour, or minutes, o seconds.

CodePudding user response:

Try using lubridate.

library(lubridate)
seconds_to_period(86400)
#[1] "1d 0H 0M 0S"

seconds_to_period(48000)
#[1] "13H 20M 0S"

The format the date:

td <- seconds_to_period(86400)
sprintf('d d:d:d', day(td), td@hour, minute(td), second(td))
#[1] "01 00:00:00"
  •  Tags:  
  • r
  • Related