gpt4 book ai didi

Javascript - 在不使用 Math.Floor 的情况下将总分钟数转换为天数、小时数和分钟数

转载 作者:行者123 更新时间:2023-12-05 03:22:50 30 4
gpt4 key购买 nike

我有总分钟数,我想将总分钟数转换为天数、小时数和分钟数格式。

var duration = getControlValue('incident','incident_open_duration');
var formattedDuration = duration/24/60 + ":" + duration/60%24 + ':' + duration%60);
alert(formattedDuration);

如果总分钟数为 8289.66,则上述代码行返回 5.756708333333333:18.161:9.659999999999854

我不需要小数,我想消除并四舍五入它们所以我使用了 Math.floor(time/24/60) + ":"+ Math.floor(time/60%24) + ':' + Math.floor(time%60) 但不幸的是我的工具不支持 Math.floor 函数

我们还有其他方法可以实现吗?

最佳答案

您可以使用双 Bitwise_NOT

var duration = 8289.66;
var cleanDuration = ~~(duration / 24 / 60) + ":" + ~~(duration / 60 % 24) + ':' + ~~(duration % 60);
console.log(cleanDuration);
//Suggested by @STh format if you need a format like HH:mm:ss
var formattedDuration = String(~~(duration / 24 / 60)).padStart(2, '0') + ":" + String(~~(duration / 60 % 24)).padStart(2, '0') + ':' + String(~~(duration % 60)).padStart(2, '0')

console.log(formattedDuration);

如果通过 padStart 的小时/分钟/秒仅为一位数字,上面的代码片段会添加前导零方法。

另一种选择是 Left_shift , Right_shiftUnsigned right shift有 0

var duration = 8289.66;
var formattedDuration = ((duration / 24 / 60) << 0) + ":" + ((duration / 60 % 24) << 0) + ':' + ((duration % 60) << 0);
console.log("Left Shift Example" , formattedDuration);

var formattedDuration = ((duration / 24 / 60) >> 0) + ":" + ((duration / 60 % 24) >> 0) + ':' + ((duration % 60) >> 0);
console.log("Rigth Shift Example" , formattedDuration);

var formattedDuration = ((duration / 24 / 60) >>> 0) + ":" + ((duration / 60 % 24) >>> 0) + ':' + ((duration % 60) >>> 0);
console.log("Unsigned right shift Example" , formattedDuration);

Bitwise_OR有 0

var duration = 8289.66;
var formattedDuration = ((duration / 24 / 60)|0) + ":" + ((duration / 60 % 24)|0) + ':' + ((duration % 60)|0)

console.log(formattedDuration);

关于Javascript - 在不使用 Math.Floor 的情况下将总分钟数转换为天数、小时数和分钟数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72614472/

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