Question

Been creating a time code format from decimal seconds but now need to get hundredths into the format.

so for 1.5 secs i need to get 00:00:01:050 but i need some help as not getting anywhere!

Any help appreciated... D.

function secondsToTime(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);

    // guesswork! 
    var divisor_for_hund = divisor_for_seconds / 100;
    var hund = divisor_for_hund;

    var obj = {
        "tc": pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2)+ ':' + pad(hund, 3),
        "h": pad(hours, 2),
        "m": pad(minutes, 2),
        "s": pad(seconds, 2)
    };
    return obj;
}
Was it helpful?

Solution

var hundredths = Math.round((secs % 1) * 100);

secs % 1 will make 0.5 from 1.5 or 0.456 from 123.456 and when you multiply that by 100, you get the number of hundredths you want to display. Then just round the result.

Update: It was pointed out you probably want ms. Just put 1000 in place of the 100.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top