Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert seconds to hours, minutes, seconds

Tags:

bash

How can I convert seconds to hours, minutes and seconds?

show_time() {   ????? }  show_time 36 # 00:00:36 show_time 1036 # 00:17:26 show_time 91925 # 25:32:05 
like image 580
Charlie Avatar asked Aug 30 '12 14:08

Charlie


People also ask

How do you convert seconds to HH mm SS?

To convert seconds to HH:MM:SS :Multiply the seconds by 1000 to get milliseconds.

What is the equation to convert seconds to hours?

There are 3,600 seconds in 1 hour. The easiest way to convert seconds to hours is to divide the number of seconds by 3,600.


1 Answers

Use date, converted to UTC:

$ date -d@36 -u +%H:%M:%S 00:00:36 $ date -d@1036 -u +%H:%M:%S 00:17:16 $ date -d@12345 -u +%H:%M:%S 03:25:45 

The limitation is the hours will loop at 23, but that doesn't matter for most use cases where you want a one-liner.

On macOS, run brew install coreutils and replace date with gdate

like image 133
ACyclic Avatar answered Sep 29 '22 06:09

ACyclic