2014-12-04 44 views
1

我想根据一天中的时间更改内容,这表明我的业务是开放还是关闭。我使用以下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点,文本显示“晚上好!目前,我们正在关闭,就可以达到我们的明天”

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

回答

0

你不能比较这样的字符串。

用途:

$hour = dt.getHours(); 

然后:

if ($hour > 10) { 
.. 
} 

0

我认为它是因为你试图让如果字符串不Numers ...

尝试如果仅使用dt.getHours():

if (dt.getHours() < 11) { 
document.write('Good morning! We are currently closed, you can reach us at 11 AM') 
} 
0

使用下列内容:

首先得到该天,小时和分钟

var dt = new Date(); 
var day = dt.getDay(); 
var hour = dt.getHours(); 
var minute= dt.getMinutes(); 

做出的日子开关:

switch(day) { 
    case 0: 
     //Be carefull! This is Sunday! 
     break; 
    case 1: 
     //Monday 
     break; 
    case 2: 
     //Thuesday 
     break; 
} 

而在情况比较时间:

case 1: 
    if (hour < 11) { 
     document.write('Good morning! We are currently closed, you can reach us at 11 AM') 
    } else if (((hour >= 16 && minutes >= 30) || hour >= 17) && hour < 18) { 
     document.write('Good afternoon! We are currently closed. You can reach us tomorrow') 
    } else if (hour >= 18) { 
     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') 
    } 
     break; 

休息应该是容易复制粘贴:)

+0

我试过这个,但控制台说有一个意外的标记案例 – Wouhero 2014-12-14 10:45:42

+0

不能帮助,如果我没有看到你创建的代码。也许你可以用你的新代码制作一个[小提琴](http://jsfiddle.net/)或者在你的问题中编辑它。 – 2014-12-15 09:14:48