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

前端 未结 30 1360
太阳男子
太阳男子 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:33
    function toHHMMSS(seconds) {
        var h, m, s, result='';
        // HOURs
        h = Math.floor(seconds/3600);
        seconds -= h*3600;
        if(h){
            result = h<10 ? '0'+h+':' : h+':';
        }
        // MINUTEs
        m = Math.floor(seconds/60);
        seconds -= m*60;
        result += m<10 ? '0'+m+':' : m+':';
        // SECONDs
        s=seconds%60;
        result += s<10 ? '0'+s : s;
        return result;
    }
    

    Examples

        toHHMMSS(111); 
        "01:51"
    
        toHHMMSS(4444);
        "01:14:04"
    
        toHHMMSS(33);
        "00:33"
    
    0 讨论(0)
  • 2020-11-22 07:37

    A regular expression can be used to match the time substring in the string returned from the toString() method of the Date object, which is formatted as follows: "Thu Jul 05 2012 02:45:12 GMT+0100 (GMT Daylight Time)". Note that this solution uses the time since the epoch: midnight of January 1, 1970. This solution can be a one-liner, though splitting it up makes it much easier to understand.

    function secondsToTime(seconds) {
        const start = new Date(1970, 1, 1, 0, 0, 0, 0).getTime();
        const end = new Date(1970, 1, 1, 0, 0, parseInt(seconds), 0).getTime();
        const duration = end - start;
    
        return new Date(duration).toString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
    }
    
    0 讨论(0)
  • 2020-11-22 07:38

    new Date().toString().split(" ")[4];

    result 15:08:03

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

    This is how i did it

    function timeFromSecs(seconds)
    {
        return(
        Math.floor(seconds/86400)+'d :'+
        Math.floor(((seconds/86400)%1)*24)+'h : '+
        Math.floor(((seconds/3600)%1)*60)+'m : '+
        Math.round(((seconds/60)%1)*60)+'s');
    }
    

    timeFromSecs(22341938) will return '258d 14h 5m 38s'

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

    I like the first answer. There some optimisations:

    • source data is a Number. additional calculations is not needed.

    • much excess computing

    Result code:

    Number.prototype.toHHMMSS = function () {
        var seconds = Math.floor(this),
            hours = Math.floor(seconds / 3600);
        seconds -= hours*3600;
        var minutes = Math.floor(seconds / 60);
        seconds -= minutes*60;
    
        if (hours   < 10) {hours   = "0"+hours;}
        if (minutes < 10) {minutes = "0"+minutes;}
        if (seconds < 10) {seconds = "0"+seconds;}
        return hours+':'+minutes+':'+seconds;
    }
    
    0 讨论(0)
  • 2020-11-22 07:43

    A Google search turned up this result:

    function secondsToTime(secs)
    {
        secs = Math.round(secs);
        var hours = Math.floor(secs / (60 * 60));
    
        var divisor_for_minutes = secs % (60 * 60);
        var minutes = Math.floor(divisor_for_minutes / 60);
    
        var divisor_for_seconds = divisor_for_minutes % 60;
        var seconds = Math.ceil(divisor_for_seconds);
    
        var obj = {
            "h": hours,
            "m": minutes,
            "s": seconds
        };
        return obj;
    }
    
    0 讨论(0)
提交回复
热议问题