How to convert time milliseconds to hours, min, sec format in JavaScript?

后端 未结 16 2387
渐次进展
渐次进展 2020-11-29 02:28

I have the time as milliseconds, but I want the time after conversion like 00:00:00.

Ex: In milliseconds=86400000. I want how many hours in that millise

16条回答
  •  悲&欢浪女
    2020-11-29 03:16

    This solution uses one function to split milliseconds into a parts object, and another function to format the parts object.

    I created 2 format functions, one as you requested, and another that prints a friendly string and considering singular/plural, and includes an option to show milliseconds.

    function parseDuration(duration) {
      let remain = duration
    
      let days = Math.floor(remain / (1000 * 60 * 60 * 24))
      remain = remain % (1000 * 60 * 60 * 24)
    
      let hours = Math.floor(remain / (1000 * 60 * 60))
      remain = remain % (1000 * 60 * 60)
    
      let minutes = Math.floor(remain / (1000 * 60))
      remain = remain % (1000 * 60)
    
      let seconds = Math.floor(remain / (1000))
      remain = remain % (1000)
    
      let milliseconds = remain
    
      return {
        days,
        hours,
        minutes,
        seconds,
        milliseconds
      };
    }
    
    function formatTime(o, useMilli = false) {
      let parts = []
      if (o.days) {
        let ret = o.days + ' day'
        if (o.days !== 1) {
          ret += 's'
        }
        parts.push(ret)
      }
      if (o.hours) {
        let ret = o.hours + ' hour'
        if (o.hours !== 1) {
          ret += 's'
        }
        parts.push(ret)
      }
      if (o.minutes) {
        let ret = o.minutes + ' minute'
        if (o.minutes !== 1) {
          ret += 's'
        }
        parts.push(ret)
    
      }
      if (o.seconds) {
        let ret = o.seconds + ' second'
        if (o.seconds !== 1) {
          ret += 's'
        }
        parts.push(ret)
      }
      if (useMilli && o.milliseconds) {
        let ret = o.milliseconds + ' millisecond'
        if (o.milliseconds !== 1) {
          ret += 's'
        }
        parts.push(ret)
      }
      if (parts.length === 0) {
        return 'instantly'
      } else {
        return parts.join(' ')
      }
    }
    
    function formatTimeHMS(o) {
      let hours = o.hours.toString()
      if (hours.length === 1) hours = '0' + hours
    
      let minutes = o.minutes.toString()
      if (minutes.length === 1) minutes = '0' + minutes
    
      let seconds = o.seconds.toString()
      if (seconds.length === 1) seconds = '0' + seconds
    
      return hours + ":" + minutes + ":" + seconds
    }
    
    function formatDurationHMS(duration) {
      let time = parseDuration(duration)
      return formatTimeHMS(time)
    }
    
    function formatDuration(duration, useMilli = false) {
      let time = parseDuration(duration)
      return formatTime(time, useMilli)
    }
    
    
    console.log(formatDurationHMS(57742343234))
    
    console.log(formatDuration(57742343234))
    console.log(formatDuration(5423401000))
    console.log(formatDuration(500))
    console.log(formatDuration(500, true))
    console.log(formatDuration(1000 * 30))
    console.log(formatDuration(1000 * 60 * 30))
    console.log(formatDuration(1000 * 60 * 60 * 12))
    console.log(formatDuration(1000 * 60 * 60 * 1))

提交回复
热议问题