gpt4 book ai didi

javascript - 使用 JavaScript 建立营业时间

转载 作者:行者123 更新时间:2023-11-30 20:11:50 30 4
gpt4 key购买 nike

我一直在尝试显示“当前在周一至周五营业”。并将更改为“当前周六至周日关闭。”

我尝试通过谷歌搜索来学习,但我无法实现:

window.onload = function status() {
var date = new Date();
console.log(date);
//var day = date.getDay();
var hour = date.getHours();// 0 = 12am, 1 = 1am, ... 18 = 6pm\
console.log(hour);

// check if it's between 9am and 11pm
if(hour > 12 ) {
document.getElementById('example').innerHTML = "Currently opened on Monday - Friday.";
} else if (hour < 23 ) {
document.getElementById('example').innerHTML = "Currently closed on Saturday - Sunday.";
} else {
console.log('Today is not a weekend and hour is between 12 - 23')
}
};

setInterval(status, 1000);
console.log(status);

最佳答案

您可以使用 Date 对象的 getDay() 方法获取星期几,然后检查它是否是星期几打开与否,如果打开则检查时间。

function status() {
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
//check if its sunday or saturday
if (day == 0 || day == 6) {
document.getElementById('example').innerHTML = "Currently closed on Saturday - Sunday.";
// check if its between 9am and 11pm (inclusive)
} else if (hour >= 9 && hour <= 23) {
document.getElementById('example').innerHTML = "Currently opened on Monday - Friday.";
} else {
console.log('Today is not a weekend and hour is between 12 - 23')
}
}

检查工作示例 https://jsfiddle.net/93ut5jve/9/引用资料:

关于javascript - 使用 JavaScript 建立营业时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52323431/

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