gpt4 book ai didi

javascript - moment.js 获取 hh :mm:ss from a decimal nr

转载 作者:行者123 更新时间:2023-11-27 22:58:37 26 4
gpt4 key购买 nike

从值 126.55(126 分 55 秒)我怎样才能得到像 02:06:55 这样的输出

var d = moment.duration(126.55,'minutes');
var hours = Math.floor(d.asHours());
var mins = Math.floor(d.asMinutes()) - hours * 60;
var secs = Math.floor(d.asSeconds()) - mins * 60;
console.log(hours + ":" + mins + ":" + secs);

我的输出是 2:6:7233

所需输出02:06:55

最佳答案

我建议使用 moment-duration-format 插件,但是,首先,秒数必须从秒转换为十进制(即:55 秒是百分之 91 秒):

window.onload = function() {
var min = 6.55;
min = parseInt(min)+ (min * 100 % 100) / 60;
var result = moment.duration(min ,'minutes').format("HH:mm:ss", {trim: false});
document.getElementById('log').textContent = result;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>

<p id="log"></p>

关于javascript - moment.js 获取 hh :mm:ss from a decimal nr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37349214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com