如何使用 jQuery 将 125 秒变成 00:02:05?
问问题
28717 次
2 回答
42
来吧!您不需要 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
}
console.log(secondsTimeSpanToHMS(125));
于 2012-08-03T09:19:47.930 回答
6
试试这个代码:
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
于 2012-08-03T09:20:07.277 回答