我试图在他们的网站上显示商店是否开放。商店每周7天营业,营业时间为07:30至22:00。现在,在answer(JSFiddle)的帮助下,我可以在07:00到22:00之间每天显示它。但我不能从07:30开始,这是最好的方法吗?
使用Javascript:
var checkOpenStatus = function () {
var d = new Date();
var date = d.getHours();
if ((date > 9) && (date < 20)) {
y = "<span style=\"color:#07ed11\">We're Open!</span>";
} else {
y = "<span style=\"color:#fc4b1c\">Sorry we're Closed.</span>";
}
document.getElementById("open-close").innerHTML = y;
};
checkOpenStatus();
HTML
<div id="open-close"></div>
答案 0 :(得分:2)
如果小时为9
,那么您还需要检查会议记录是否大于30
。
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
if ((h > 9 || (h == 9 && m >= 30)) && (h < 20)) {
下面是函数的自定义版本,它注入了证明逻辑的日期。运行脚本并检查控制台。
var checkOpenStatus = function (d) {
//var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
if ((h > 9 || (h == 9 && m >= 30)) && (h < 20)) {
y = "<span style=\"color:#07ed11\">We're Open!</span>";
} else {
y = "<span style=\"color:#fc4b1c\">Sorry we're Closed.</span>";
}
console.log(d, y);
};
checkOpenStatus(new Date(2017, 1, 1, 9, 0));
checkOpenStatus(new Date(2017, 1, 1, 9, 10));
checkOpenStatus(new Date(2017, 1, 1, 9, 20));
checkOpenStatus(new Date(2017, 1, 1, 9, 30));
checkOpenStatus(new Date(2017, 1, 1, 9, 40));
checkOpenStatus(new Date(2017, 1, 1, 10, 40));
checkOpenStatus(new Date(2017, 1, 1, 20, 00));
checkOpenStatus(new Date(2017, 1, 1, 23, 00));
&#13;