How can I convert milliseconds to “hhmmss” format using javascript?

前端 未结 6 2013
花落未央
花落未央 2021-01-18 02:11

I am using javascript Date object trying to convert millisecond to how many hour, minute and second it is.

I have the currentTime in milliseconds

var         


        
相关标签:
6条回答
  • 2021-01-18 02:23

    Convert ms to hh:mm:ss

    function millisecondsToHuman(ms) {
      const seconds = Math.floor((ms / 1000) % 60);
      const minutes = Math.floor((ms / 1000 / 60) % 60);
      const hours = Math.floor((ms  / 1000 / 3600 ) % 24)
    
      const humanized = [
        pad(hours.toString(), 2),
        pad(minutes.toString(), 2),
        pad(seconds.toString(), 2),
      ].join(':');
    
      return humanized;
    }
    =
    
    0 讨论(0)
  • 2021-01-18 02:29
    var secDiff = timeDiff / 1000; //in s
    var minDiff = timeDiff / 60 / 1000; //in minutes
    var hDiff = timeDiff / 3600 / 1000; //in hours  
    

    updated

    function msToHMS( ms ) {
        // 1- Convert to seconds:
        var seconds = ms / 1000;
        // 2- Extract hours:
        var hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
        seconds = seconds % 3600; // seconds remaining after extracting hours
        // 3- Extract minutes:
        var minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
        // 4- Keep only seconds not extracted to minutes:
        seconds = seconds % 60;
        alert( hours+":"+minutes+":"+seconds);
    }
    
    var timespan = 2568370873; 
    msToHMS( timespan );  
    

    Demo

    0 讨论(0)
  • 2021-01-18 02:41
    var timediff = futureTime - currentTime
    long seconds = (long) (timediff / 1000) % 60 ;
    long minutes = (long) ((timediff / (1000*60)) % 60);
    long hours   = (long) ((timediff / (1000*60*60)) % 24);
    if(hours>0)
        time = hours+" hrs : "+minutes+" mins";
    else if(minutes>0)
        time = minutes+" mins";
    else if(seconds>0)
        time = seconds+" secs";
    
    0 讨论(0)
  • 2021-01-18 02:44
    function msToHMS( 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 ;
    }
    
    0 讨论(0)
  • 2021-01-18 02:45

    The difference in time is in milliseconds: Get time difference between two dates in seconds

    to get the difference you have to use math.floor() http://www.w3schools.com/jsref/jsref_floor.asp

    var secDiff = Math.floor(timeDiff / 1000); //in s
    var minDiff = Math.floor(timeDiff / 60 / 1000); //in minutes
    var hDiff = Math.floor(timeDiff / 3600 / 1000); //in hours
    
    0 讨论(0)
  • 2021-01-18 02:47

    If you are confident that the period will always be less than a day you could use this one-liner:

    new Date(timeDiff).toISOString().slice(11,19)   // HH:MM:SS
    

    N.B. This will be wrong if timeDiff is greater than a day.

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