I am adding functionality to a website to change the message depending on if a food truck is open. I was successfully able to make the message change depending on the time, but I'm having trouble implementing getDay() to show the closed message all day on Saturday and Sunday.
我正在为网站添加功能,以根据食品卡车是否打开来更改消息。我成功地根据时间改变了消息,但是我无法实现getDay()以在周六和周日全天显示已关闭的消息。
Here is the working script that I have so far:
这是我到目前为止的工作脚本:
<script language="JavaScript">
var mess1="";
var outmess= "Kendo's Cuisine "
document.write("<center><font size=+1><i><b>")
day = new Date( )
hr = day.getHours( )
if (( hr >= 0 ) && (hr <= 11 ))
mess1= "is closed right now. He's open Mon - Fri 11am - 2pm. "
if (( hr >= 11 ) && (hr < 13))
mess1=" is open right now! Call in your order to have it ready by the time you get here!"
if (( hr >= 13) && (hr <= 14))
mess1= "usually runs out of food by now! Call before you come!"
if (( hr >= 14 ) && (hr <= 24 ))
mess1= "is closed right now. He's open Mon - Fri 11am - 2pm. "
document.write("<blink>")
document.write(outmess)
document.write("</blink>")
document.write(mess1)
document.write("</b></i></font></center>")
</script>
2 个解决方案
#1
1
It seems you want to put up a "closed" message outside the hours of 11:00 to 14:00 Monday to Friday, so perhaps:
看来你想在星期一到星期五的11:00到14:00之间发出“关闭”的信息,所以也许:
function isOpen(date) {
var day = date.getDay();
var hour = date.getHours();
if (day == 0 || day == 6 || hour < 11 || hour > 13) {
// shop is closed
return false;
}
// Otherwise, the shop is open
return true;
}
Note however that if the date object is created on the client, it will be local to that timezone, which may not match wherever the shop is. So you probably need to do this based on UTC time, which will be consistent everywhere.
但请注意,如果在客户端上创建了日期对象,则它将是该时区的本地对象,在商店所在的任何地方都可能无法匹配。因此,您可能需要根据UTC时间执行此操作,这将在任何地方保持一致。
#2
0
Use getDay() method to get the weekday from date object. It returns a number from 0 - 6 to indicate days from sunday - saturday.
使用getDay()方法从date对象获取工作日。它返回0到6之间的数字,表示从星期日到星期六的天数。
So you have to check like
所以你必须检查一下
var day = new Date();
if(day.getDay() == 0 || day.getDay() == 6) {
alert("shop is closed");
}
#1
1
It seems you want to put up a "closed" message outside the hours of 11:00 to 14:00 Monday to Friday, so perhaps:
看来你想在星期一到星期五的11:00到14:00之间发出“关闭”的信息,所以也许:
function isOpen(date) {
var day = date.getDay();
var hour = date.getHours();
if (day == 0 || day == 6 || hour < 11 || hour > 13) {
// shop is closed
return false;
}
// Otherwise, the shop is open
return true;
}
Note however that if the date object is created on the client, it will be local to that timezone, which may not match wherever the shop is. So you probably need to do this based on UTC time, which will be consistent everywhere.
但请注意,如果在客户端上创建了日期对象,则它将是该时区的本地对象,在商店所在的任何地方都可能无法匹配。因此,您可能需要根据UTC时间执行此操作,这将在任何地方保持一致。
#2
0
Use getDay() method to get the weekday from date object. It returns a number from 0 - 6 to indicate days from sunday - saturday.
使用getDay()方法从date对象获取工作日。它返回0到6之间的数字,表示从星期日到星期六的天数。
So you have to check like
所以你必须检查一下
var day = new Date();
if(day.getDay() == 0 || day.getDay() == 6) {
alert("shop is closed");
}