Javascript counter formatting at 00:26

I have Javascript code that is basically an expired timer to download a file. Here is the code:

// Update elapsed
var time = new Date().getTime() - startTime;
var elapsed = Math.floor(time / 100) / 10;
console.log(elapsed);

Using this, I get logs in the console looking like: 0.7, 0.8, 0.9, 1, 1.1, 1.2etc. These are seconds, and the number after .is 10 seconds of a second. I want to format this in a more understandable form for a person, for example, 26 seconds will be 00:26, 1 minute 30 seconds will be 01:30, 20 minutes will be 20:00, etc.

However, I have no idea how to write the function correctly in order to convert it into a readable form.

+3
source share
2 answers

http://jsfiddle.net/69dgJ/1/

function formatSecs(secs) {
    secs = parseInt(secs);
    ss = secs % 60;
    mm = Math.floor(secs / 60.0);
    if(ss < 10) { ss = "0" + ss; }
    if(mm < 10) { mm = "0" + mm; }
    fmt = mm + ":" + ss;
    return fmt;
}
+2
source
0

All Articles