如何在 JavaScript 中将时间以毫秒为单位转换为小时、分钟、秒格式? [英] How to convert time in milliseconds to hours, min, sec format in JavaScript?
本文介绍了如何在 JavaScript 中将时间以毫秒为单位转换为小时、分钟、秒格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以毫秒为单位的时间,我想将其转换为 HH:MM:SS
格式.它应该环绕, milliseconds = 86400000
我想得到 00:00:00
.
I have a time as a number of milliseconds and I want to convert it to a HH:MM:SS
format. It should wrap around, with milliseconds = 86400000
I want to get 00:00:00
.
推荐答案
如何创建这样的函数:
function msToTime(duration) {
var milliseconds = Math.floor((duration % 1000) / 100),
seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}
console.log(msToTime(300000))
这篇关于如何在 JavaScript 中将时间以毫秒为单位转换为小时、分钟、秒格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文