Convert seconds to HH-MM-SS with JavaScript?

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

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

30条回答
  •  故里飘歌
    2020-11-22 10:20

    Chiming in on this old thread -- the OP stated HH:MM:SS, and many of the solutions work, until you realize you need more than 24 hours listed. And maybe you don't want more than a single line of code. Here you go:

    d=(s)=>{f=Math.floor;g=(n)=>('00'+n).slice(-2);return f(s/3600)+':'+g(f(s/60)%60)+':'+g(s%60)}
    

    It returns H+:MM:SS. To use it, simply use:

    d(91260);     // returns "25:21:00"
    d(960);       // returns "0:16:00"
    

    ...I tried to get it to use the least amount of code possible, for a nice one-liner approach.

提交回复
热议问题