Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Displaying seconds as days/hours/mins/seconds?

Is it possible to easily format seconds as a human-readable time in bash?

I don't want to format it as a date, but as the number of days/hours/minutes, etc...
by

1 Answer

Amit8z4mc
You can use something like this:
function displaytime {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(( $D > 0 )) && printf '%d days ' $D
(( $H > 0 )) && printf '%d hours ' $H
(( $M > 0 )) && printf '%d minutes ' $M
(( $D > 0 || $H > 0 || $M > 0 )) && printf 'and '
printf '%d seconds\n' $S
}

Login / Signup to Answer the Question.