Coder Perfect

JavaScript converts seconds to a time string in the hh:mm:ss format.

Problem

I’d like to convert a time interval, such as a number of seconds, to a colon-separated time string (hh:mm:ss)

I found some helpful responses here, however they all refer to converting to the x hours and x minutes format.

Is there a jQuery snippet or just raw JavaScript that performs this?

Asked by medk

Solution #1

String.prototype.toHHMMSS = function () {
    var sec_num = parseInt(this, 10); // don't forget the second param
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours+':'+minutes+':'+seconds;
}

You can now utilize it in the following ways:

alert("5678".toHHMMSS());

Working snippet:

Answered by powtac

Solution #2

You may achieve this without using an external JS library by using the JS Date method, as seen below:

Answered by Harish Anchu

Solution #3

You can use this regular expression to get the time portion in the format hh:MM:ss:

(This was mentioned above in same post by someone, thanks for that.)

Answered by Raj

Solution #4

Ordinary javascript with the Date object is my recommendation. (See the second code sample for a faster solution that uses toTimeString.)

var seconds = 9999;
// multiply by 1000 because Date() requires miliseconds
var date = new Date(seconds * 1000);
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getSeconds();
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
// if (hh > 12) {hh = hh % 12;}
// These lines ensure you have two-digits
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
if (ss < 10) {ss = "0"+ss;}
// This formats your string to HH:MM:SS
var t = hh+":"+mm+":"+ss;
document.write(t);

(Of course, the Date object you construct will have a real date associated with it, but that data is irrelevant for these purposes.)

Split on the whitespace and use the toTimeString function:

var seconds = 9999; // Some arbitrary value
var date = new Date(seconds * 1000); // multiply by 1000 because Date() requires miliseconds
var timeStr = date.toTimeString().split(' ')[0];

’16:54:58 GMT-0800 (PST)’ is returned by toTimeString, and ’16:54:58′ is returned by splitting on the first whitespace.

Answered by JellicleCat

Solution #5

This was discovered through a Google search.

function secondsToTime(secs)
{
    secs = Math.round(secs);
    var hours = Math.floor(secs / (60 * 60));

    var divisor_for_minutes = secs % (60 * 60);
    var minutes = Math.floor(divisor_for_minutes / 60);

    var divisor_for_seconds = divisor_for_minutes % 60;
    var seconds = Math.ceil(divisor_for_seconds);

    var obj = {
        "h": hours,
        "m": minutes,
        "s": seconds
    };
    return obj;
}

Answered by Ash Burlaczenko

Post is based on https://stackoverflow.com/questions/6312993/javascript-seconds-to-time-string-with-format-hhmmss