How to use format() on a moment.js duration?

前端 未结 28 2220
自闭症患者
自闭症患者 2020-11-27 04:34

Is there any way I can use the moment.js format method on duration objects? I can\'t find it anywhere in the docs and it doesn\'t seen to be an attribute on du

28条回答
  •  萌比男神i
    2020-11-27 04:54

    How about native javascript?

    var formatTime = function(integer) {
        if(integer < 10) {
            return "0" + integer; 
        } else {
            return integer;
        }
    }
    
    function getDuration(ms) {
        var s1 = Math.floor(ms/1000);
        var s2 = s1%60;
        var m1 = Math.floor(s1/60);
        var m2 = m1%60;
        var h1 = Math.floor(m1/60);
        var string = formatTime(h1) +":" + formatTime(m2) + ":" + formatTime(s2);
        return string;
    }
    

提交回复
热议问题