gpt4 book ai didi

javascript - 如何计算小时:Minute format from provided Start Hour-Minute to End Hours-Minute?的持续时间

转载 作者:行者123 更新时间:2023-11-28 01:30:32 24 4
gpt4 key购买 nike

Javascript 中,我有 4 个带有值的变量。

以下是示例:

var start_hour = 1;
var start_minute = 00;
var end_hour = 6;
var end_minute = 30;

我想创建一个可以计算持续时间并打印的 JavaScript 函数,如上面的示例所示,持续时间输出应为 5:30 表示,5 小时 30 分钟 .

最佳答案

请参阅这个 JSFiddle http://jsfiddle.net/sajith/LkZC7/

var start_hour = 1;
var start_minute = 00;
var end_hour = 6;
var end_minute = 30;

start_minute = parseInt(start_minute, 10);
end_minute = parseInt(end_minute, 10);

if (start_hour < end_hour ) {
var start_mins = start_hour * 60 + start_minute;
var end_mins = end_hour * 60 + end_minute;
var dur_mins = end_mins - start_mins;
var dur_hours = Math.floor(dur_mins / 60);
dur_mins = dur_mins % 60;
} else {
// Start hour is greater than end hour
}

alert(dur_hours + ":" + dur_mins);

关于javascript - 如何计算小时:Minute format from provided Start Hour-Minute to End Hours-Minute?的持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22165245/

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