我目前正在使用我在上一篇文章中找到的一些脚本..根据日期和时间显示打开或关闭..
$(document).ready(function() {
"use strict";
var Now = new Date();
var CurrentDay = Now.getDay();
// opening time - 24 hours so 9:30am is 9, 30
var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 8);
// closing time - 24 hours so 5:30pm is 17, 30
var ClosingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 20);
var Open = (Now.getTime() > OpeningTime.getTime() && Now.getTime() < ClosingTime.getTime());
// days 0.sun 1.mon 2.tues 3.wed 4.thur 5.fri 6.sat
// CurrentDay !== 0 && the # is the day to eclude, so if I want to be closed on Sat6, Sun0, Wed3
// CurrentDay !== 6 && CurrentDay !== 0 && CurrentDay !== 3 && Open
if (CurrentDay !== 1 && CurrentDay !== 5 && Open) {
$('.openstatus').toggle();
}
});
目前设定为周一至周五上午8点至晚上8点..但我也希望在周六上午9点至下午5点至周日上午10点至下午4点显示公开文本
我们非常感谢任何建议。
非常感谢。
答案 0 :(得分:1)
我在这里有一个建议: https://jsfiddle.net/xncor0b8/3/
var startingHour = {
1: 8,
2: 8,
3: 8,
4: 8,
5: 8,
6: 9,
0: 10
};
var startingMin = {
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
0: 0
}
var closingHour = {
1: 20,
2: 20,
3: 20,
4: 20,
5: 20,
6: 17,
0: 16
};
var closingMin = {
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
0: 0
};
var CurrentDay = Now.getDay();
var startingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), startingHour[CurrentDay], startingMin[CurrentDay]);
var closingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), closingHour[CurrentDay], closingMin[CurrentDay]);
var Open = (Now.getTime() > startingTime.getTime() && Now.getTime() < closingTime.getTime());
if (Open) {
$('.openstatus').toggle();
}
将商店的营业时间和分钟定义为var,并使用配置检查当前时间。
如果打开/关闭时间可能会改变,似乎更容易更新。
答案 1 :(得分:0)
这样的事情对你有用吗?
$(document).ready(function() {
// Date time variables
var dtNow; // Now
var dtOpenWD; // Weekday - Open
var dtCloseWD; // Weekday - Close
var dtOpenSat; // Saturday - Open
var dtCloseSat; // Saturday - Close
var dtOpenSun; // Sunday - Open
var dtCloseSun; // Sunday - Close
var isOpen; // is open?
// Initialize date values
dtNow = new Date();
dtOpenWD = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 8);
dtCloseWD = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 20);
dtOpenSat = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 9);
dtCloseSat = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 17);
dtOpenSun = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 10);
dtCloseSun = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 16);
// Determine day and calculate isOpen
switch (dtNow.getDay()) {
case 0:
isOpen = (dtNow.getTime() > dtOpenSun.getTime() && dtNow.getTime() < dtCloseSun.getTime());
break;
case 6:
isOpen = (dtNow.getTime() > dtOpenSat.getTime() && dtNow.getTime() < dtCloseSat.getTime());
break;
default:
isOpen = (dtNow.getTime() > dtOpenWD.getTime() && dtNow.getTime() < dtCloseWD.getTime());
}
if (isOpen) {
$('.openstatus').toggle();
}
});
答案 2 :(得分:0)
在代码中遵循相同的逻辑,您可以为星期六和星期日添加单独的开始和结束时间。
// opening time - 24 hours so 9:30am is 9, 30
// Saturday Opening Time
if (CurrentDay == 6)
var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 9);
// Sunday Opening Time
else if (CurrentDay == 0)
var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 10);
// Weekday Opening Time
else
var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 8);
JSFiddle:https://jsfiddle.net/xncor0b8/5/