Javascript 把时间分成更小的部分
我有一个视频的持续时间(秒)Javascript 把时间分成更小的部分,javascript,math,time,Javascript,Math,Time,我有一个视频的持续时间(秒)634.567!我已使用以下代码成功地将其转换为HH:MM:SS:ms格式: var pad = function(num, size) { return ('000' + num).slice(size * -1); }, time = parseFloat(timeInSeconds).toFixed(3), hours = Math.floor(time / 60 / 60), minutes = Math.floor(time / 60) % 60, seco
634.567
!我已使用以下代码成功地将其转换为HH:MM:SS:ms格式:
var pad = function(num, size) { return ('000' + num).slice(size * -1); },
time = parseFloat(timeInSeconds).toFixed(3),
hours = Math.floor(time / 60 / 60),
minutes = Math.floor(time / 60) % 60,
seconds = Math.floor(time - minutes * 60),
milliseconds = time.slice(-3);
return pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2) + ',' + pad(milliseconds, 3);
这将返回“00:10:34567”
现在,我想把这段时间分成5秒的间隔。因此,它变成了一个JS数组,如下所示
00.00.05,000 | 00.00.10000 | 00.00.15,000 |直到数组中的最后一行是00:10:34567(因此它都是等分的)
如何将时间分割成更小的时间,然后将其推送到数组中 因此,最好使用原始的持续时间
timeinsectonds
,以5秒的步长迭代,并转换每个步长的相应值,例如:
var pad = function(num, size) { return ('000' + num).slice(size * -1); };
function conv(t){
var time = parseFloat(t).toFixed(3),
hours = Math.floor(time / 60 / 60),
minutes = Math.floor(time / 60) % 60,
seconds = Math.floor(time - minutes * 60),
milliseconds = time.slice(-3);
return pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2) + ',' + pad(milliseconds, 3);
}
var timeInSeconds = 634.567;
//required step
var delta = 5;
/*
the condition (t < timeInSeconds + delta) in combination
with Math.min(t, timeInSeconds) ensures that the for
loop will consider the value timeInSeconds even if it
is not divisible by delta
*/
var arr = [];
for(var t = 0; t < timeInSeconds + delta; t += delta){
arr.push(conv(Math.min(t, timeInSeconds)));
}
console.log(arr);
您是否尝试添加额外的行:seconds=Math.ceil(seconds/5)*5@杰夫:谢谢你的回答——你是什么意思?我试着加上那个,但它只给了我634!成功了!非常感谢:)
[ '00:00:00,000', '00:00:05,000', '00:00:10,000', '00:00:11,000' ]