Convert seconds to HH-MM-SS with JavaScript?

Hannoun Yassir picture Hannoun Yassir · Aug 24, 2009 · Viewed 305.4k times · Source

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

Answer

Harish Anchu picture Harish Anchu · Aug 13, 2014

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);