gpt4 book ai didi

javascript - 我如何在周末使用 javascript 更改图片?

转载 作者:行者123 更新时间:2023-11-30 10:35:26 25 4
gpt4 key购买 nike

我想在聊天可用时显示在我们的网站上。问题是它不会在周末开放。我如何排除它们?可以吗?

<html>
<body>
<script type="text/javascript">
function chatonoff(){
var now = new Date();
var hour = now.getHours();

if (hour >=9 && hour <=18)
{
document.getElementById("chat").src = "/bilder/butik/chat-open.png";
}
}
</script>
<img id="chat" src="/bilder/butik/chat.png" onload="chatonoff()">

</body>
</html>

最佳答案

你可以这样做

function chatonoff(){
var now = new Date();
var hour = now.getHours();
var day = now.getDay();

//Check if weekend : in this case, I assume that saturday == 6 and Sunday = 0
//It depends on your location / timezone
if(day != 6 && day != 0)
{
if (hour >=9 && hour <=18)
{
document.getElementById("chat").src = "/bilder/butik/chat-open.png";
}
}
}

编辑

关于排除假期:

您应该创建一个包含所有关闭日期的数组。然后,检查数组中是否存在当前日期

  var offDaysListArray = ['2013-01-01','2013-01-02'];

var now = new Date();
var y = now .getFullYear();
var d = (now .getDate() < 10) ? '0'+now .getDate() : now .getDate();
var m = ((now .getMonth()+1) < 10) ? '0'+(now .getMonth()+1) : (now .getMonth()+1);

//Check if it is a closed day
if(offDaysListArray.indexOf(y + '-' + m + '-' + d) != -1)
return false; //It is a close day

请注意indexOf,IE8 等旧浏览器没有实现此功能。检查Why doesn't indexOf work on an array IE8?

关于javascript - 我如何在周末使用 javascript 更改图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14273514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com