英文:
Elapsed ms to day?
问题
我将为您翻译代码部分:
我正在将经过的毫秒转换为HH:mm:ss,但如果经过的毫秒数超过一天,我将失去[这][1]信息:
const elapsedSeconds = 218509
const elapsed = moment.utc(elapsedSeconds * 1000).format('HH:mm:ss');
alert(elapsed); // 打印 12:41:49
如何在HH:mm:ss附近的地方显示从毫秒中获取的天数?
在这种情况下,有60小时,所以它应该打印2(天):12:41:49。或者至少是60:41:49。
[1]: http://jsfiddle.net/Ldoxtchq/
希望这有所帮助!如果您需要进一步的帮助,请随时提问。
英文:
I'm converting elapsed ms to HH:mm:ss, but if the elapsed ms are higher of a day, I lost that info:
const elapsedSeconds = 218509
const elapsed = moment.utc(elapsedSeconds * 1000).format('HH:mm:ss');
alert(elapsed); // print 12:41:49
How can I also display days from ms, near the HH:mm:ss?
In this case there are 60hours, so it should print 2(days):12:41:49. Or 60:41:49 at least.
答案1
得分: 2
根据您需要的输出,您可以使用moment
库将日期时间转换为指定的格式。
您的代码如下:
const elapsedMilliseconds = 218509000;
const duration = moment.duration(elapsedMilliseconds);
const elapsed = duration.days() + " (days): " + duration.hours() + ":" + duration.minutes() + ":" + duration.seconds();
alert(elapsed);
结果:
2 (days): 12:41:49
如果您想使用JavaScript来获取总的小时:分钟:秒,可以使用以下代码:
function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}
function convertMsToHM(milliseconds) {
let seconds = Math.floor(milliseconds / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
seconds = seconds % 60;
minutes = seconds >= 30 ? minutes + 1 : minutes;
minutes = minutes % 60;
return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}:${padTo2Digits(seconds)}`;
}
console.log(convertMsToHM(218509000));
结果:
"60:42:49"
英文:
So based on your required ouput you can use moment library to convert date time to specified format.
For that your code looks like :
const elapsedMilliseconds = 218509000;
const duration = moment.duration(elapsedMilliseconds);
const elapsed = duration.days() + "(days):" + duration.hours() + ":" + duration.minutes() + ":" + duration.seconds();
alert(elapsed);
Result :
2(days):12:41:49
If you want to do by javascript to get total HH:mm:ss then :
function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}
function convertMsToHM(milliseconds) {
let seconds = Math.floor(milliseconds / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
seconds = seconds % 60;
minutes = seconds >= 30 ? minutes + 1 : minutes;
minutes = minutes % 60;
return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}:${padTo2Digits(seconds)}`;
}
console.log(convertMsToHM(218509000));
Result :
"60:42:49"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论