Convert seconds to HH-MM-SS with JavaScript?

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

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

相关标签:
30条回答
  • 2020-11-22 10:11

    This function should do it :

    var convertTime = function (input, separator) {
        var pad = function(input) {return input < 10 ? "0" + input : input;};
        return [
            pad(Math.floor(input / 3600)),
            pad(Math.floor(input % 3600 / 60)),
            pad(Math.floor(input % 60)),
        ].join(typeof separator !== 'undefined' ?  separator : ':' );
    }
    

    Without passing a separator, it uses : as the (default) separator :

    time = convertTime(13551.9941351); // --> OUTPUT = 03:45:51
    

    If you want to use - as a separator, just pass it as the second parameter:

    time = convertTime(1126.5135155, '-'); // --> OUTPUT = 00-18-46
    

    See also this Fiddle.

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

    Try this:

    function toTimeString(seconds) {
      return (new Date(seconds * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];
    }
    
    0 讨论(0)
  • 2020-11-22 10:13

    There are lots of options of solve this problem, and obvious there are good option suggested about, But I wants to add one more optimized code here

    function formatSeconds(sec) {
         return [(sec / 3600), ((sec % 3600) / 60), ((sec % 3600) % 60)]
                .map(v => v < 10 ? "0" + parseInt(v) : parseInt(v))
                .filter((i, j) => i !== "00" || j > 0)
                .join(":");
    }
    

    if you don't wants formatted zero with less then 10 number, you can use

    function formatSeconds(sec) {
      return parseInt(sec / 3600) + ':' + parseInt((sec % 3600) / 60) + ':' + parseInt((sec % 3600) % 60);
    

    }

    Sample Code http://fiddly.org/1c476/1

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

    Here is a function to convert seconds to hh-mm-ss format based on powtac's answer here

    jsfiddle

    /** 
     * Convert seconds to hh-mm-ss format.
     * @param {number} totalSeconds - the total seconds to convert to hh- mm-ss
    **/
    var SecondsTohhmmss = function(totalSeconds) {
      var hours   = Math.floor(totalSeconds / 3600);
      var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
      var seconds = totalSeconds - (hours * 3600) - (minutes * 60);
    
      // round seconds
      seconds = Math.round(seconds * 100) / 100
    
      var result = (hours < 10 ? "0" + hours : hours);
          result += "-" + (minutes < 10 ? "0" + minutes : minutes);
          result += "-" + (seconds  < 10 ? "0" + seconds : seconds);
      return result;
    }
    

    Example use

    var seconds = SecondsTohhmmss(70);
    console.log(seconds);
    // logs 00-01-10
    
    0 讨论(0)
  • 2020-11-22 10:15

    I just wanted to give a little explanation to the nice answer above:

    var totalSec = new Date().getTime() / 1000;
    var hours = parseInt( totalSec / 3600 ) % 24;
    var minutes = parseInt( totalSec / 60 ) % 60;
    var seconds = totalSec % 60;
    
    var result = (hours < 10 ? "0" + hours : hours) + "-" + (minutes < 10 ? "0" + minutes : minutes) + "-" + (seconds  < 10 ? "0" + seconds : seconds);
    

    On the second line, since there are 3600 seconds in 1 hour, we divide the total number of seconds by 3600 to get the total number of hours. We use parseInt to strip off any decimal. If totalSec was 12600 (3 and half hours), then parseInt( totalSec / 3600 ) would return 3, since we will have 3 full hours. Why do we need the % 24 in this case? If we exceed 24 hours, let's say we have 25 hours (90000 seconds), then the modulo here will take us back to 1 again, rather than returning 25. It is confining the result within a 24 hour limit, since there are 24 hours in one day.

    When you see something like this:

    25 % 24
    

    Think of it like this:

    25 mod 24 or what is the remainder when we divide 25 by 24
    
    0 讨论(0)
  • 2020-11-22 10:17

    I don't think any built-in feature of the standard Date object will do this for you in a way that's more convenient than just doing the math yourself.

    hours = Math.floor(totalSeconds / 3600);
    totalSeconds %= 3600;
    minutes = Math.floor(totalSeconds / 60);
    seconds = totalSeconds % 60;
    

    Example:

    let totalSeconds = 28565;
    let hours = Math.floor(totalSeconds / 3600);
    totalSeconds %= 3600;
    let minutes = Math.floor(totalSeconds / 60);
    let seconds = totalSeconds % 60;
    
    console.log("hours: " + hours);
    console.log("minutes: " + minutes);
    console.log("seconds: " + seconds);
    
    // If you want strings with leading zeroes:
    minutes = String(minutes).padStart(2, "0");
    hours = String(hours).padStart(2, "0");
    seconds = String(seconds).padStart(2, "0");
    console.log(hours + ":" + minutes + ":" + seconds);

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