我当前正在使用在上一篇文章中找到的一些脚本。它将根据日期和时间显示打开或关闭。
$(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点之间显示公开文本
任何建议将不胜感激。
非常感谢你。
https://jsfiddle.net/xncor0b8/
最佳答案
这样的事情对您有用吗?
$(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();
}
});