我是一名初学者,我主要专注于HTML。我目前正在为我朋友的电台节目制作一个网站。现在我希望在美国东部时间周五下午5:00到6:00的某个时间和星期几显示某个div。我知道这可能涉及JavaScript,所以我四处搜索并找到了这个。但由于某种原因,在陈述的时间里,我得到了相反的结果。
var Now = new Date();
var CurrentDay = Now.getDay();
// opening time - military time so 5:00pm is 17, 00
var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 17, 00);
// closing time - military time so 6:00pm is 18, 00
var ClosingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 19, 00);
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 !== 0 && CurrentDay !== 1 && CurrentDay !== 2 && CurrentDay !== 4 && CurrentDay !== 5 && CurrentDay !== 6 && Open) {
$('.openstatus').toggle();
}
&#13;
<div class="hours openstatus">Radio-show is live</div>
<div class="closed openstatus">Tune in at Wednesday from 5:00 PM - 6:00 PM</div>
&#13;
答案 0 :(得分:0)
您可以使用以下方法简化您的Javascript:
monitor
您可能还需要执行类似添加课程&#39;隐藏&#39;到您的div,或更改您的CSS以if (new Date().getDay() === 3 && open) {
$('.openstatus').show();
}
同样按惯例,所有变量都应遵循.closed { display: none; }
,而不是以大写字母开头。