How can I convert milliseconds to “hhmmss” format using javascript?

前端 未结 6 2012
花落未央
花落未央 2021-01-18 02:11

I am using javascript Date object trying to convert millisecond to how many hour, minute and second it is.

I have the currentTime in milliseconds

var         


        
6条回答
  •  清酒与你
    2021-01-18 02:29

    var secDiff = timeDiff / 1000; //in s
    var minDiff = timeDiff / 60 / 1000; //in minutes
    var hDiff = timeDiff / 3600 / 1000; //in hours  
    

    updated

    function msToHMS( ms ) {
        // 1- Convert to seconds:
        var seconds = ms / 1000;
        // 2- Extract hours:
        var hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
        seconds = seconds % 3600; // seconds remaining after extracting hours
        // 3- Extract minutes:
        var minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
        // 4- Keep only seconds not extracted to minutes:
        seconds = seconds % 60;
        alert( hours+":"+minutes+":"+seconds);
    }
    
    var timespan = 2568370873; 
    msToHMS( timespan );  
    

    Demo

提交回复
热议问题