I have this function which formats seconds to time
 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);     return minutes + ":" + seconds;  }   it works great but i need a function to turn milliseconds to time and I cant seem to understand what i need to do to this function to return time in this format
mm:ss.mill 01:28.5568 
                Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp) . The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.
To convert milliseconds to hours and minutes:Divide the milliseconds by 1000 to get the seconds. Divide the seconds by 60 to get the minutes. Divide the minutes by 60 to get the hours.
JavaScript - Date getMilliseconds() Method Javascript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.
If TimeUnit or toMinutes are unsupported (such as on Android before API version 9), use the following equations: int seconds = (int) (milliseconds / 1000) % 60 ; int minutes = (int) ((milliseconds / (1000*60)) % 60); int hours = (int) ((milliseconds / (1000*60*60)) % 24); //etc...
Lots of unnecessary flooring in other answers. If the string is in milliseconds, convert to h:m:s as follows:
function msToTime(s) {   var ms = s % 1000;   s = (s - ms) / 1000;   var secs = s % 60;   s = (s - secs) / 60;   var mins = s % 60;   var hrs = (s - mins) / 60;    return hrs + ':' + mins + ':' + secs + '.' + ms; }   If you want it formatted as hh:mm:ss.sss then use:
function msToTime(s) {      // Pad to 2 or 3 digits, default is 2    function pad(n, z) {      z = z || 2;      return ('00' + n).slice(-z);    }      var ms = s % 1000;    s = (s - ms) / 1000;    var secs = s % 60;    s = (s - secs) / 60;    var mins = s % 60;    var hrs = (s - mins) / 60;      return pad(hrs) + ':' + pad(mins) + ':' + pad(secs) + '.' + pad(ms, 3);  }    console.log(msToTime(55018))  Using some recently added language features, the pad function can be more concise:
function msToTime(s) {      // Pad to 2 or 3 digits, default is 2    var pad = (n, z = 2) => ('00' + n).slice(-z);    return pad(s/3.6e6|0) + ':' + pad((s%3.6e6)/6e4 | 0) + ':' + pad((s%6e4)/1000|0) + '.' + pad(s%1000, 3);  }    // Current hh:mm:ss.sss UTC  console.log(msToTime(new Date() % 8.64e7))  If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With