milliseconds to time in javascript

后端 未结 18 2180
遇见更好的自我
遇见更好的自我 2020-12-02 12:16

I have this function which formats seconds to time

 function secondsToTime(secs){
    var hours = Math.floor(secs / (60 * 60));
    var divisor_for_minutes          


        
18条回答
  •  忘掉有多难
    2020-12-02 12:39

    Editing RobG's solution and using JavaScript's Date().

    function msToTime(ms) {
    
        function addZ(n) {
            return (n<10? '0':'') + n;
        }
        var dt = new Date(ms);
        var hrs = dt.getHours();
        var mins = dt.getMinutes();
        var secs = dt.getSeconds();
        var millis = dt.getMilliseconds();
    
        var tm = addZ(hrs) + ':' + addZ(mins) + ':' + addZ(secs) + "." + millis;
        return tm;
    }
    

提交回复
热议问题