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

前端 未结 6 2016
花落未央
花落未央 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;
    }
    =
    

提交回复
热议问题