我想根据一天中的时间更改内容,这表明我的商家是营业还是休息。我使用以下 JavaScript:
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
enter code here
if (day <= 0) {
if (time < "11:00:00") {
document.write('Good morning! We are currently closed, you can reach us at 11 AM')
} else if (time > "16:30:00" && time < "18:00:00") {
document.write('Good afternoon! We are currently closed. You can reach us tomorrow')
} else if (time > "18:00:00") {
document.write('Good evening! We are currently closed. You can reach us tomorrow')
} else {
document.write('Customer service: <i class="fa fa-phone"></i> +1 555.234.987')
}
} else if (day <= 1) {
if (time < "09:00:00") {
document.write('Good morning! We are currently closed, you can reach us at 9 AM')
} else if (time > "19:00:00") {
document.write('Good evening! We are currently closed, you can reach us tomorrow')
} else {
document.write('Customer Service: <i class="fa fa-phone"></i> +1 555.234.987')
}
}
</script>
周一到周日依此类推。
由于某种原因,文本显示了另一条文本,因此当周一上午 10 点时,文本显示“晚上好!我们目前已关闭,您可以明天联系我们”
我知道我做错了什么,但我不知道是什么......
最佳答案
您不能像这样比较字符串。
用途:
$hour = dt.getHours();
然后:
if ($hour > 10) {
..
}
等等
关于javascript - 如何根据一天中的时间更改消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293107/