我正在尝试使用以下等式格式化HTML5 currentTime属性:
var s = parseInt(audio.currentTime % 60);
var m = parseInt((audio.currentTime / 60)%60); duration.innerHTML = m +':'+ s;
有效,只有我希望秒1-9显示为:01 - :09,而不是:1和9,就像他们目前所做的那样。我该如何编写此代码?
答案 0 :(得分:19)
这对你有帮助,我用过:
function formatTime(seconds) {
minutes = Math.floor(seconds / 60);
minutes = (minutes >= 10) ? minutes : "0" + minutes;
seconds = Math.floor(seconds % 60);
seconds = (seconds >= 10) ? seconds : "0" + seconds;
return minutes + ":" + seconds;
}
答案 1 :(得分:1)
if (m < 10) m = '0' + m;
if (s < 10) s = '0' + s;
答案 2 :(得分:0)
如果s
小于10,则只需添加一个0。
var m = parseInt((audio.currentTime / 60) % 60);
把
if (s < 10) {
s = '0' + s;
}
代码很简单。
答案 3 :(得分:0)
var currentTime = audio.currentTime | 0;
var minutes = "0" + Math.floor(currentTime / 60);
var seconds = "0" + (currentTime - minutes * 60);
var cur = minutes.substr(-2) + ":" + seconds.substr(-2);
答案 4 :(得分:0)
在TypeScript上:
formatTime(seconds: number): string {
let minutes: any = Math.floor(seconds / 60);
let secs: any = Math.floor(seconds % 60);
if (minutes < 10) {
minutes = '0' + minutes;
}
if (secs < 10) {
secs = '0' + secs;
}
return minutes + ':' + secs;
}