如何显示"We are Open/Closed"在一个基于日期和时间的网页上。
我在这里找到了代码,但是它没有考虑时区。如何修改脚本,使其根据时区显示打开/关闭状态?
到目前为止我所知道的。
<span id="hoursofoperation"></span>
<script>
var a = document.getElementById("hoursofoperation");
var d = new Date();
var n = d.getDay();
var now = d.getHours() + "." + d.getMinutes();
var weekdays = [
["Sunday", 9.30, 12.00, 15.30,22.00],
["Monday", 8.30, 12.00, 15.30,19.00],
["Tuesday", 8.30, 12.00, 15.30,19.00],
["Wednesday", 8.30, 12.00, 15.30,19.00],
["Thursday", 8.30, 12.00, 15.30,19.00],
["Friday", 8.30, 11.30],
["Saturday"] // we are closed, sorry!
];
var day = weekdays[n];
if (now > day[1] && now < day[2] || now > day[3] && now < day[4]) {
a.innerHTML = "We are Open.";
}
else {
a.innerHTML = "We are Closed.";
}
</script>
用.getUTCHours
和.getUTCMinutes
代替。
确保将您的表也转换为UTC时间。