Quantcast
Channel: JavaScript seconds to time string with format hh:mm:ss - Stack Overflow
Viewing all articles
Browse latest Browse all 49

Answer by webinista for JavaScript seconds to time string with format hh:mm:ss

$
0
0

If you know the number of seconds you have, this will work. It also uses the native Date() object.

function formattime(numberofseconds){        var zero = '0', hours, minutes, seconds, time;    time = new Date(0, 0, 0, 0, 0, numberofseconds, 0);    hh = time.getHours();    mm = time.getMinutes();    ss = time.getSeconds()     // Pad zero values to 00    hh = (zero+hh).slice(-2);    mm = (zero+mm).slice(-2);    ss = (zero+ss).slice(-2);    time = hh +':'+ mm +':'+ ss;    return time; }

Viewing all articles
Browse latest Browse all 49

Trending Articles