Convert seconds to HH-MM-SS with JavaScript?

前端 未结 30 2052
南旧
南旧 2020-11-22 10:05

How can I convert seconds to an HH-MM-SS string using JavaScript?

相关标签:
30条回答
  • 2020-11-22 10:19
    var sec_to_hms = function(sec){
    var min, hours;
         sec = sec - (min = Math.floor(sec/60))*60;
         min = min - (hours = Math.floor(min/60))*60;
         return (hours?hours+':':'') + ((min+'').padStart(2, '0')) + ':'+ ((sec+'').padStart(2, '0'));
    }
    alert(sec_to_hms(2442542));
    
    0 讨论(0)
  • 2020-11-22 10:20

    Chiming in on this old thread -- the OP stated HH:MM:SS, and many of the solutions work, until you realize you need more than 24 hours listed. And maybe you don't want more than a single line of code. Here you go:

    d=(s)=>{f=Math.floor;g=(n)=>('00'+n).slice(-2);return f(s/3600)+':'+g(f(s/60)%60)+':'+g(s%60)}
    

    It returns H+:MM:SS. To use it, simply use:

    d(91260);     // returns "25:21:00"
    d(960);       // returns "0:16:00"
    

    ...I tried to get it to use the least amount of code possible, for a nice one-liner approach.

    0 讨论(0)
  • 2020-11-22 10:21

    I ran into the case some have mentioned where the number of seconds is more than a day. Here's an adapted version of @Harish Anchu's top-rated answer that accounts for longer periods of time:

    function secondsToTime(seconds) {
      const arr = new Date(seconds * 1000).toISOString().substr(11, 8).split(':');
    
      const days = Math.floor(seconds / 86400);
      arr[0] = parseInt(arr[0], 10) + days * 24;
    
      return arr.join(':');
    }
    

    Example:

    secondsToTime(101596) // outputs '28:13:16' as opposed to '04:13:16'
    
    0 讨论(0)
  • 2020-11-22 10:21

    None of the answers here satisfies my requirements as I want to be able to handle

    1. Large numbers of seconds (days), and
    2. Negative numbers

    Although those are not required by the OP, it's good practice to cover edge cases, especially when it takes little effort.

    It's pretty obvious is that the OP means a NUMBER of seconds when he says seconds. Why would peg your function on String?

    function secondsToTimeSpan(seconds) {
        const value = Math.abs(seconds);
        const days = Math.floor(value / 1440);
        const hours = Math.floor((value - (days * 1440)) / 3600);
        const min = Math.floor((value - (days * 1440) - (hours * 3600)) / 60);
        const sec = value - (days * 1440) - (hours * 3600) - (min * 60);
        return `${seconds < 0 ? '-':''}${days > 0 ? days + '.':''}${hours < 10 ? '0' + hours:hours}:${min < 10 ? '0' + min:min}:${sec < 10 ? '0' + sec:sec}`
    }
    secondsToTimeSpan(0);       // => 00:00:00
    secondsToTimeSpan(1);       // => 00:00:01
    secondsToTimeSpan(1440);    // => 1.00:00:00
    secondsToTimeSpan(-1440);   // => -1.00:00:00
    secondsToTimeSpan(-1);      // => -00:00:01
    
    0 讨论(0)
  • 2020-11-22 10:23

    Here is an extension to Number class. toHHMMSS() converts seconds to an hh:mm:ss string.

    Number.prototype.toHHMMSS = function() {
      var hours = Math.floor(this / 3600) < 10 ? ("00" + Math.floor(this / 3600)).slice(-2) : Math.floor(this / 3600);
      var minutes = ("00" + Math.floor((this % 3600) / 60)).slice(-2);
      var seconds = ("00" + (this % 3600) % 60).slice(-2);
      return hours + ":" + minutes + ":" + seconds;
    }
    
    // Usage: [number variable].toHHMMSS();
    
    // Here is a simple test
    var totalseconds = 1234;
    document.getElementById("timespan").innerHTML = totalseconds.toHHMMSS();
    // HTML of the test
    <div id="timespan"></div>

    0 讨论(0)
  • 2020-11-22 10:25

    In one line, using T.J. Crowder's solution :

    secToHHMMSS = seconds => `${Math.floor(seconds / 3600)}:${Math.floor((seconds % 3600) / 60)}:${Math.floor((seconds % 3600) % 60)}`
    

    In one line, another solution that also count days :

    secToDHHMMSS = seconds => `${parseInt(seconds / 86400)}d ${new Date(seconds * 1000).toISOString().substr(11, 8)}`
    

    Source : https://gist.github.com/martinbean/2bf88c446be8048814cf02b2641ba276

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