Elapsed ms to day?

huangapple go评论51阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年2月8日 16:47:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定