javascript - 如何根据一天中的时间更改消息?

标签 javascript jquery date time

我想根据一天中的时间更改内容,这表明我的商家是营业还是休息。我使用以下 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: &nbsp;&nbsp; <i class="fa fa-phone"></i> &nbsp;+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: &nbsp;&nbsp; <i class="fa fa-phone"></i> &nbsp;+1 555.234.987')
        }
    } 
</script>

周一到周日依此类推。

由于某种原因,文本显示了另一条文本,因此当周一上午 10 点时,文本显示“晚上好!我们目前已关闭,您可以明天联系我们”

我知道我做错了什么,但我不知道是什么......

最佳答案

您不能像这样比较字符串。

用途:

$hour = dt.getHours();

然后:

if ($hour > 10) {
 ..
}

等等

关于javascript - 如何根据一天中的时间更改消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293107/

相关文章:

javascript - 在 capybara 中,如何让#find等待具有相同类的不同元素出现?

javascript - AngularJS 从表中收集数据 - 解析为 JSON

ruby-on-rails - 在 Rails 3.1 中更改 created_at 的格式?

c - strptime() 在 C 中产生垃圾时间

javascript - 响应式菜单打开

javascript - 在像 jQuery 这样的 Javascript 对象中启用可链接性

javascript - 如何检测用户是否拥有来自网站的移动应用程序并打开该应用程序

javascript - jQuery .each() 方法不会遍历所有图像

javascript - 遍历从 JQuery XML 对象返回的子对象的子对象

转换为 big int 时出现 PHP 日期格式问题