我想根据一天中的时间更改内容,这可以告诉我我的公司是否营业。我使用以下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) {
..
}
等等