使用jQuery将秒转为H:M:S格式

时间:2012-08-03 09:11:45

标签: javascript jquery html

  

可能重复:
  convert seconds to HH-MM-SS with javascript?

如何使用jQuery将125秒说成00:02:05?

2 个答案:

答案 0 :(得分:39)

来吧!你不需要jQuery来实现这一点:-) 这是一个可能的片段:

function secondsTimeSpanToHMS(s) {
    var h = Math.floor(s/3600); //Get whole hours
    s -= h*3600;
    var m = Math.floor(s/60); //Get remaining minutes
    s -= m*60;
    return h+":"+(m < 10 ? '0'+m : m)+":"+(s < 10 ? '0'+s : s); //zero padding on minutes and seconds
}

secondsTimeSpanToHMS(125);

答案 1 :(得分:5)

试试这段代码:

function getTime(seconds) {

    //a day contains 60 * 60 * 24 = 86400 seconds
    //an hour contains 60 * 60 = 3600 seconds
    //a minut contains 60 seconds
    //the amount of seconds we have left
    var leftover = seconds;

    //how many full days fits in the amount of leftover seconds
    var days = Math.floor(leftover / 86400);

    //how many seconds are left
    leftover = leftover - (days * 86400);

    //how many full hours fits in the amount of leftover seconds
    var hours = Math.floor(leftover / 3600);

    //how many seconds are left
    leftover = leftover - (hours * 3600);

    //how many minutes fits in the amount of leftover seconds
    var minutes = Math.floor(leftover / 60);

    //how many seconds are left
    leftover = leftover - (minutes * 60);
    document.write(days + ':' + hours + ':' + minutes + ':' + leftover);
}

<强>测试

getTime(2490453);​ //-> 28:19:47.55:2853