Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

milliseconds to time in javascript

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 
like image 789
Matt Elhotiby Avatar asked Mar 19 '12 00:03

Matt Elhotiby


People also ask

How do you convert milliseconds to timestamps?

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.

How do you convert milliseconds to hours and minutes?

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.

How do you write milliseconds in Javascript?

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.

How do you convert milliseconds to hours in Java?

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...


1 Answers

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))
like image 113
RobG Avatar answered Sep 23 '22 20:09

RobG