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

前端 未结 30 1357
太阳男子
太阳男子 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:23

    I recommend ordinary javascript, using the Date object:

    var seconds = 9999;
    // multiply by 1000 because Date() requires miliseconds
    var date = new Date(seconds * 1000);
    var hh = date.getUTCHours();
    var mm = date.getUTCMinutes();
    var ss = date.getSeconds();
    // If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
    // if (hh > 12) {hh = hh % 12;}
    // These lines ensure you have two-digits
    if (hh < 10) {hh = "0"+hh;}
    if (mm < 10) {mm = "0"+mm;}
    if (ss < 10) {ss = "0"+ss;}
    // This formats your string to HH:MM:SS
    var t = hh+":"+mm+":"+ss;
    document.write(t);
    

    (Of course, the Date object created will have an actual date associated with it, but that data is extraneous, so for these purposes, you don't have to worry about it.)

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

    Variation on a theme. Handles single digit seconds a little differently

    seconds2time(0)  ->  "0s" 
    seconds2time(59) -> "59s" 
    seconds2time(60) -> "1:00" 
    seconds2time(1000) -> "16:40" 
    seconds2time(4000) -> "1:06:40"
    
    function seconds2time (seconds) {
        var hours   = Math.floor(seconds / 3600);
        var minutes = Math.floor((seconds - (hours * 3600)) / 60);
        var seconds = seconds - (hours * 3600) - (minutes * 60);
        var time = "";
    
        if (hours != 0) {
          time = hours+":";
        }
        if (minutes != 0 || time !== "") {
          minutes = (minutes < 10 && time !== "") ? "0"+minutes : String(minutes);
          time += minutes+":";
        }
        if (time === "") {
          time = seconds+"s";
        }
        else {
          time += (seconds < 10) ? "0"+seconds : String(seconds);
        }
        return time;
    }
    
    0 讨论(0)
  • 2020-11-22 07:25

    There's a new method for strings on the block: padStart

    const str = '5';
    str.padStart(2, '0'); // 05
    

    Here is a sample use case: YouTube durations in 4 lines of JavaScript

    0 讨论(0)
  • 2020-11-22 07:26
    s2t=function (t){
      return parseInt(t/86400)+'d '+(new Date(t%86400*1000)).toUTCString().replace(/.*(\d{2}):(\d{2}):(\d{2}).*/, "$1h $2m $3s");
    }
    
    s2t(123456);
    

    result:

    1d 10h 17m 36s
    
    0 讨论(0)
  • 2020-11-22 07:26

    I think performance wise this is by far the fastest:

    var t = 34236; // your seconds
    var time = ('0'+Math.floor(t/3600) % 24).slice(-2)+':'+('0'+Math.floor(t/60)%60).slice(-2)+':'+('0' + t % 60).slice(-2)
    //would output: 09:30:36
    
    0 讨论(0)
  • 2020-11-22 07:29

    Here's my take on it:

    function formatTime(seconds) {
      const h = Math.floor(seconds / 3600);
      const m = Math.floor((seconds % 3600) / 60);
      const s = Math.round(seconds % 60);
      return [
        h,
        m > 9 ? m : (h ? '0' + m : m || '0'),
        s > 9 ? s : '0' + s
      ].filter(Boolean).join(':');
    }
    

    Expected results:

    const expect = require('expect');
    expect(formatTime(0)).toEqual('0:00');
    expect(formatTime(1)).toEqual('0:01');
    expect(formatTime(599)).toEqual('9:59');
    expect(formatTime(600)).toEqual('10:00');
    expect(formatTime(3600)).toEqual('1:00:00');
    expect(formatTime(360009)).toEqual('100:00:09');
    expect(formatTime(0.2)).toEqual('0:00');
    
    0 讨论(0)
提交回复
热议问题