Convert seconds to HH-MM-SS with JavaScript?

Solution 1:

You can manage to do this without any external JavaScript library with the help of JavaScript Date method like following:

var date = new Date(null);
date.setSeconds(SECONDS); // specify value for SECONDS here
var result = date.toISOString().substr(11, 8);

Or, as per @Frank's comment; a one liner:

new Date(SECONDS * 1000).toISOString().substr(11, 8);

Solution 2:

Updated (2020):

Please use @Frank's one line solution:

new Date(SECONDS * 1000).toISOString().substr(11, 8)

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

new Date(SECONDS * 1000).toISOString().substr(14, 5)

It is by far the best solution.


Old answer:

Use the Moment.js library.