Javascript 基于日期和时间,显示/隐藏元素
我想基于多个日期和时间在页面上显示和隐藏元素。例如,我有一个警报框,我想在太平洋标准时间上午9点到下午12点之间的以下日期4/28、4/29和4/30显示。在剩下的几个小时里躲起来。发布的代码适用于打开和关闭时间,但我不确定如何添加多个日期。或者我只是错过了什么 编辑:我在jQuery/JavaScript方面很差劲 HTMLJavascript 基于日期和时间,显示/隐藏元素,javascript,jquery,Javascript,Jquery,我想基于多个日期和时间在页面上显示和隐藏元素。例如,我有一个警报框,我想在太平洋标准时间上午9点到下午12点之间的以下日期4/28、4/29和4/30显示。在剩下的几个小时里躲起来。发布的代码适用于打开和关闭时间,但我不确定如何添加多个日期。或者我只是错过了什么 编辑:我在jQuery/JavaScript方面很差劲 HTML <div class="open openstatus">Open</div> <div class="closed openstatus"
<div class="open openstatus">Open</div>
<div class="closed openstatus">Closed</div>
<div id="alertBox">My alert box</div>
打开
关闭
JS
var now = new Date(),
currentDay = now.getDay(),
openTime = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
13,
02
),
closeTime = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
13,
03
),
open = now.getTime() > openTime.getTime() && now.getTime() < closeTime.getTime();
if (currentDay !== 6 && currentDay !== 0 && open) {
$('.openstatus').toggle();
}
var now=new Date(),
currentDay=now.getDay(),
开放时间=新日期(
现在。getFullYear(),
现在。getMonth(),
现在。getDate(),
13,
02
),
关闭时间=新日期(
现在。getFullYear(),
现在。getMonth(),
现在。getDate(),
13,
03
),
open=now.getTime()>openTime.getTime()&&now.getTime()你不能添加这样的东西吗
currentDate=now.getDate()
如果(currentDay!==6&¤tDay!==0&&open&&(currentDate==28 | | currentDate==29 | | currentDate==30))
这会检查当前日期是28、29还是30。我会这样做:
HTML
<div class="open openstatus">Open</div>
<div class="closed openstatus">Closed</div>
<div id="alertBox">My alert box</div>
我的警报框
JAVASCRIPT
function myFunc(){
var d = new Date(); //client Date
d.setMinutes(d.getMinutes() + d.getTimezoneOffset()); //To get current GMT time
d.setHours(d.getHours() - 8); // This is time in PST
var day = d.getDay(),
month = d.getMonth(),
hours = d.getHours(),
dateOfMonth = d.getDate();
var prohibbittedMonth = 4;
var prohibbittedDates =[28, 29, 30];
var showAlertBox = false;
if(day === 0 || day === 6){
showAlertBox = true; // If the day is Saturday or Sunday
}else if(month = prohibbittedMonth && prohibbittedDates.includes(dateOfMonth) && !(hours >= 0 && hours <9)){
showAlertBox = true;
}
// You may combine the above two condition in a single line. I have just separated them for better readability.
if(!showAlertBox){
document.getElementById('alertBox').style.display = "none"; //You may use your jquery here if you like
}else{
document.getElementById('alertBox').style.display = ""; //You may use your jquery here if you like
}
}
myFunc();
函数myFunc(){
var d=新日期();//客户端日期
d、 setMinutes(d.getMinutes()+d.getTimezoneOffset());//获取当前GMT时间
d、 setHours(d.getHours()-8);//这是PST中的时间
var day=d.getDay(),
month=d.getMonth(),
小时数=d.getHours(),
dateOfMonth=d.getDate();
var ProhibitedMonth=4;
var ProhibitedDates=[28,29,30];
var showAlertBox=false;
如果(天===0 | |天===6){
showAlertBox=true;//如果是星期六或星期天
}else if(month=prohibbitedmonth&&prohibbiteddates.includes(dateOfMonth)&&&!(hours>=0&&hours)有两件事-1.您可能需要使用类似于moment.js的东西,因为您使用的日期格式将特定于本地时区2。在您的if条件下,您可能只需要使用.show()
而不是.toggle()
由于前者的条件满足,然后使用else
来.hide()
@RahulDwivedi谢谢你的建议。我会考虑它们。@RahulDwivedi如果我使用MomentJS,我会用矩().date()替换我的现在的.getDay()
?很抱歉,我不知道该如何添加。moment.js
有不同的方法来处理这些要求。@alysus看到下面的完整答案,并告诉我是否有帮助。我为回复太晚而道歉。感谢您提供了扩展的答案。如果可能,您能解释一下您的代码与on的代码之间的差异吗我发布了什么?我知道你提到了时区…所以,在阿肯色州查看我的页面的人会在他们的时区看到它?对不起,我只是想了解发生了什么。@alyus我做了一些遗漏的事情:1.我调整了时区,这样就不会影响基于应用程序所在时区的结果ewed 2.简化了逻辑以使其更具可扩展性-比如说在将来,您应该能够添加多个禁止日期,只需将它们添加到prohibitedDates
数组中即可。月份也是如此。3.我怀疑您的切换
逻辑可能不起作用。我的代码中已经注意到了这一点。4.代码与何时显示所有我的代码看起来更干净。如果有效,请告诉我。