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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11792726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:06:26  来源:igfitidea点击:

Turn seconds into H:M:S format using jQuery

javascriptjqueryhtml

提问by user1559555

Possible Duplicate:
convert seconds to HH-MM-SS with javascript?

可能的重复:
使用 javascript 将秒数转换为 HH-MM-SS?

How can I turn say 125 seconds into 00:02:05 using jQuery?

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

回答by Claudix

Come on! You don't need jQuery to achieve that :-) Here it is a possible snippet:

来吧!你不需要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);

回答by Sandy8086

try this code:

试试这个代码:

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);
}

Test:

测试:

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