javascriptdatetimedate-formattime-format

Convert seconds to HH-MM-SS with JavaScript?


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


Solution

  • Updated (2020):

    Please use @Frank's one line solution:

    new Date(SECONDS * 1000).toISOString().substring(11, 16)
    

    If SECONDS<3600 and if you want to show only MM:SS then use below code:

    new Date(SECONDS * 1000).toISOString().substring(14, 19)
    

    It is by far the best solution.


    Old answer:

    Use the Moment.js library.