Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert decimal hours to HH:MM:SS

I'm looking for a way to convert decimal hours to HH:MM:SS

For instance, as input:

4.927778 hours

Desired output:

04:55:40
like image 563
user2757171 Avatar asked Dec 16 '22 04:12

user2757171


2 Answers

You can try something like below

dh <- 4.927778
strftime(as.POSIXct(dh * 60 * 60, origin = Sys.Date(), tz = "GMT"), format = "%H:%M:%S")
## [1] "04:55:40"
like image 62
CHP Avatar answered Dec 27 '22 11:12

CHP


You should be able to get an idea of what you need to do -

a <- "4.927778 hours"
a <- as.numeric(gsub(x = a,pattern = " hours", replacement = ""))

h <- a%/% 1
m <- ((a%% 1)*60) %/% 1
s <- round((((a%% 1)*60) %% 1)*60,0)
paste(h,m,s,sep = ":")
#[1] "4:55:40"
like image 29
TheComeOnMan Avatar answered Dec 27 '22 12:12

TheComeOnMan