JavaScript seconds to time string with format hh:mm:ss

前端 未结 30 1444
太阳男子
太阳男子 2020-11-22 07:19

I want to convert a duration of time, i.e., number of seconds to colon-separated time string (hh:mm:ss)

I found some useful answers here but they all talk about conv

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

    Here is a fairly simple solution that rounds to the nearest second!

    var returnElapsedTime = function(epoch) {
      //We are assuming that the epoch is in seconds
      var hours = epoch / 3600,
          minutes = (hours % 1) * 60,
          seconds = (minutes % 1) * 60;
      return Math.floor(hours) + ":" + Math.floor(minutes) + ":" + Math.round(seconds);
    }

    0 讨论(0)
  • 2020-11-22 07:48

    You can manage to do this without any external JS library with the help of JS Date method like following:

    var date = new Date(0);
    date.setSeconds(45); // specify value for SECONDS here
    var timeString = date.toISOString().substr(11, 8);
    console.log(timeString)

    0 讨论(0)
  • 2020-11-22 07:48

    You can use Momement.js with moment-duration-format plugin:

    var seconds = 3820;
    var duration = moment.duration(seconds, 'seconds');
    var formatted = duration.format("hh:mm:ss");
    console.log(formatted); // 01:03:40
    <!-- Moment.js library -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
    
    <!-- moment-duration-format plugin -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>

    See also this Fiddle

    0 讨论(0)
  • 2020-11-22 07:48
    secToHHMM(number: number) {
        debugger;
        let hours = Math.floor(number / 3600);
        let minutes = Math.floor((number - (hours * 3600)) / 60);
        let seconds = number - (hours * 3600) - (minutes * 60);
        let H, M, S;
        if (hours < 10) H = ("0" + hours);
        if (minutes < 10) M = ("0" + minutes);
        if (seconds < 10) S = ("0" + seconds);
        return (H || hours) + ':' + (M || minutes) + ':' + (S || seconds);
    }
    
    0 讨论(0)
  • 2020-11-22 07:49

    Using the amazing moment.js library:

    function humanizeDuration(input, units ) { 
      // units is a string with possible values of y, M, w, d, h, m, s, ms
      var duration = moment().startOf('day').add(units, input),
        format = "";
    
      if(duration.hour() > 0){ format += "H [hours] "; }
    
      if(duration.minute() > 0){ format += "m [minutes] "; }
    
      format += " s [seconds]";
    
      return duration.format(format);
    }
    

    This allows you to specify any duration be it hours, minutes, seconds, mills, and returns a human readable version.

    0 讨论(0)
  • 2020-11-22 07:50

    I loved Powtac's answer, but I wanted to use it in angular.js, so I created a filter using his code.

    .filter('HHMMSS', ['$filter', function ($filter) {
        return function (input, decimals) {
            var sec_num = parseInt(input, 10),
                decimal = parseFloat(input) - sec_num,
                hours   = Math.floor(sec_num / 3600),
                minutes = Math.floor((sec_num - (hours * 3600)) / 60),
                seconds = sec_num - (hours * 3600) - (minutes * 60);
    
            if (hours   < 10) {hours   = "0"+hours;}
            if (minutes < 10) {minutes = "0"+minutes;}
            if (seconds < 10) {seconds = "0"+seconds;}
            var time    = hours+':'+minutes+':'+seconds;
            if (decimals > 0) {
                time += '.' + $filter('number')(decimal, decimals).substr(2);
            }
            return time;
        };
    }])
    

    It's functionally identical, except that I added in an optional decimals field to display fractional seconds. Use it like you would any other filter:

    {{ elapsedTime | HHMMSS }} displays: 01:23:45

    {{ elapsedTime | HHMMSS : 3 }} displays: 01:23:45.678

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