JSFiddle - React, Tailwind, and code Playground

by saatchi

JavaScript

/* Milliseconds to H:M:S format */
function msToHMS(ms) {
    var secondsOrig = Math.round(ms / 1000);
    var h = Math.floor(secondsOrig / 3600);
    secondsOrig = secondsOrig % 3600;
    var m = Math.floor(secondsOrig / 60);
    secondsOrig = secondsOrig % 60;
    var s = secondsOrig;
    return {
        h: h,
        m: m,
        s: s,
        toString: function () {
            return this.h + ":" + this.m + ":" + this.s;
        }
    };
}

function until23H(ms) {
    var h23ms = 23 * 3600 * 1000;
    if (ms > h23ms) throw "More than 23 hours was sent to until23H function";
    return {
        orig: msToHMS(ms),
        until23H: msToHMS(h23ms - ms)
    };
}


console.log(until23H(11 * 3600000 + 22 * 60000 + 33 * 1000).orig.toString());
console.log(until23H(1 * 3600000).until23H.toString());