gpt4 book ai didi

javascript - 检查时间是否在2个时间之间

转载 作者:行者123 更新时间:2023-12-01 00:18:49 24 4
gpt4 key购买 nike

应用程序必须根据当天的当前时间显示门是打开还是关闭的消息。

大门在上午 8:00 至上午 9:30 之前或下午 5:50 后至晚上 9:00 之前开放,其余时间大门保持关闭状态。我想根据输入的时间显示消息。下面的代码输出是意外且不正确的。

function isValid(date, h1, m1, h2, m2) {
    var h = date.getHours();
    var m = date.getMinutes();
    return (h1 < h || h1 == h && m1 <= m) && (h < h2 || h == h2 && m <= m2);
}


function a() {
    var current = new Date('2020-01-03 09:31:00');
    if ((isValid(current, 8, 0, 9, 30)) || (isValid(current, 5, 50, 21, 0))) {
        return 'Gate is open'
    } else {
        return 'Please come after 8:00am and before 9:30am OR after 5:50pm and before 9:00pm';
    }
}a();

最佳答案

您必须在下午日期中使用 17 而不是 5

function isValid(date, h1, m1, h2, m2) {
var h = date.getHours();
var m = date.getMinutes();
return (h1 <= h && m1 <= m) && (h <= h2 && m <= m2);
}


function a() {
var current = new Date('2020-01-03 09:31:00');
if ((isValid(current, 8, 0, 9, 30)) || (isValid(current, 17, 50, 21, 0))){
return 'Gate is open'
} else {
return 'Please come after 8:00am and before 9:30am OR after 5:50pm and before 9:00pm';
}
}

关于javascript - 检查时间是否在2个时间之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59579965/

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