【发布时间】:2018-10-10 16:58:17
【问题描述】:
我正在尝试获取每个 mp3 声音的持续时间,但有时它会显示持续时间,而有时它会显示无效。
我想以hh:mm:ss 格式打印时间。
我正在显示持续时间的 Div 块:
var duration17 = document.getElementById("audio-17").duration;
duration17 = secondstotime(duration17);
document.getElementById("duration-17").innerHTML = duration17;
我将秒数转换为正确时间的函数:
//convert seconds to the proper time duration
function secondstotime(secs)
{
var t = new Date(1970,0,1);
t.setSeconds(secs);
var s = t.toTimeString().substr(0,8);
if(secs > 86399)
s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
return s;
}
【问题讨论】:
-
你在这里寻找什么样的输出?如果您想要像
61 seconds = 1:01这样的时间,那么该函数看起来是一种非常复杂且容易出错的方法...... -
输出应该类似于 hour:min:ss 即 00:10:13
-
这个问题有答案。如果可以使用,建议
moment.js,如果不能,建议使用其中的单线解决方案。不错@AndrewLohr -
@brabster 不,我也尝试了 momentjs,但仍然抛出此错误。
标签: javascript date datetime time