Convert seconds to HH-MM-SS with JavaScript?

前端 未结 30 2590
南旧
南旧 2020-11-22 10:05

How can I convert seconds to an HH-MM-SS string using JavaScript?

30条回答
  •  攒了一身酷
    2020-11-22 10:36

    This does the trick:

    function secondstotime(secs)
    {
        var t = new Date(1970,0,1);
        t.setSeconds(secs);
        var s = t.toTimeString().substr(0,8);
        if(secs > 86399)
            s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
        return s;
    }
    

    (Sourced from here)

提交回复
热议问题