Simple way to convert HH:MM:SS (hours:minutes:seconds.split seconds) to seconds

前端 未结 12 1579
走了就别回头了
走了就别回头了 2021-01-31 16:40

What\'s an easy way to convert 00:20:40.28 (HH:MM:SS) to seconds with a Bash script?

Split seconds can be cut out, it’s not essential.

12条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 17:19

    Try this:

    T='00:20:40.28'
    SavedIFS="$IFS"
    IFS=":."
    Time=($T)
    Seconds=$((${Time[0]}*3600 + ${Time[1]}*60 + ${Time[2]})).${Time[3]}
    IFS="$SavedIFS"
    
    echo $Seconds
    

    ($) splits based on the splitter (IFS).

    ${[]} returns the element of the at the .

    $(()) performs the arithmetic expression.

    Hope this helps.

提交回复
热议问题