JavaScript seconds to time string with format hh:mm:ss

前端 未结 30 1452
太阳男子
太阳男子 2020-11-22 07:19

I want to convert a duration of time, i.e., number of seconds to colon-separated time string (hh:mm:ss)

I found some useful answers here but they all talk about conv

30条回答
  •  死守一世寂寞
    2020-11-22 07:26

    I think performance wise this is by far the fastest:

    var t = 34236; // your seconds
    var time = ('0'+Math.floor(t/3600) % 24).slice(-2)+':'+('0'+Math.floor(t/60)%60).slice(-2)+':'+('0' + t % 60).slice(-2)
    //would output: 09:30:36
    

提交回复
热议问题