Convert seconds to hours, minutes, seconds

前端 未结 13 989
梦谈多话
梦谈多话 2020-12-02 14:08

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:0         


        
相关标签:
13条回答
  • 2020-12-02 15:03

    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

    0 讨论(0)
提交回复
热议问题