milliseconds to time in javascript

后端 未结 18 2103
遇见更好的自我
遇见更好的自我 2020-12-02 12:16

I have this function which formats seconds to time

 function secondsToTime(secs){
    var hours = Math.floor(secs / (60 * 60));
    var divisor_for_minutes          


        
相关标签:
18条回答
  • 2020-12-02 12:47

    A possible solution that worked for my case. It turns milliseconds into hh:ss time:

    function millisecondstotime(ms) {
    var x = new Date(ms);
    var y = x.getHours();
    if (y < 10) {
    y = '0' + y;
    } 
    var z = x.getMinutes();
    if (z < 10) {
        z = '0' + z;
    } 
    return y + ':' + z;
    }
    
    0 讨论(0)
  • 2020-12-02 12:53

    Here is a filter that use:

    app.filter('milliSecondsToTimeCode', function () {
        return function msToTime(duration) {
            var milliseconds = parseInt((duration % 1000) / 100)
                , seconds = parseInt((duration / 1000) % 60)
                , minutes = parseInt((duration / (1000 * 60)) % 60)
                , hours = parseInt((duration / (1000 * 60 * 60)) % 24);
    
            hours = (hours < 10) ? "0" + hours : hours;
            minutes = (minutes < 10) ? "0" + minutes : minutes;
            seconds = (seconds < 10) ? "0" + seconds : seconds;
    
            return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
        };
    });
    

    Just add it to your expression as such

    {{milliseconds | milliSecondsToTimeCode}}
    
    0 讨论(0)
  • 2020-12-02 12:54

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

    0 讨论(0)
  • 2020-12-02 12:55
    function millisecondsToTime(millisecs){
      var ms = Math.abs(millisecs) % 1000;
      var secs = (millisecs < 0 ? -1 : 1) * ((Math.abs(millisecs) - ms) / 1000);
      ms = '' + ms;
      ms = '000'.substring(ms.length) + ms;
      return secsToTime(secs) + '.' + ms;
    }
    
    0 讨论(0)
  • 2020-12-02 12:56

    This is the solution I got and working so good!

    function msToHuman(duration) {
    var milliseconds = parseInt((duration%1000)/100)
        seconds = parseInt((duration/1000)%60)
        minutes = parseInt((duration/(1000*60))%60)
        hours = parseInt((duration/(1000*60*60))%24);
    
    
    return hours + "hrs " minutes + "min " + seconds + "sec " + milliseconds + 'ms';
    }
    
    0 讨论(0)
  • 2020-12-02 12:56

    Most of the answers don't cover cases where there is more than 24h. This one does. I suggest extending Date object:

    class SuperDate extends Date {
      get raceTime() {
        return Math.floor(this/36e5).toString().padStart(2,'0')
        + this.toISOString().slice(13, -1)
      }
    }
    
    console.log('marathon', new SuperDate(11235200).raceTime)
    console.log('ironman', new SuperDate(40521100).raceTime)
    console.log('spartathlon', new SuperDate(116239000).raceTime)
    console.log('epoch', new SuperDate(new Date()).raceTime)

    This approach works great with Firestore Timestamp objects which are similar to what you need:

    class SuperDate extends Date {
      fromFirestore (timestamp) {
        return new SuperDate(timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000)
      }
      get raceTime() {
        return Math.floor(this/36e5).toString().padStart(2,'0')
        + this.toISOString().slice(13, -1)
      }
    }
    
    const timestamp = {seconds: 11235, nanoseconds: 200000000}
    
    console.log('timestamp', new SuperDate().fromFirestore(timestamp))
    console.log('marathon', new SuperDate().fromFirestore(timestamp).raceTime)

    0 讨论(0)
提交回复
热议问题