如何在if else语句中声明两个条件

时间:2015-10-16 20:27:03

标签: javascript if-statement

嘿我想要在零售店的网站上显示一个打开的指示器(非常像Yelp的指示“立即打开”)。我已经想出如何每天设定几小时(例如下面的例子)。

但该店周五的收盘时间不同。星期六,&星期日休息。我希望有希望找到一种方法来将if / else这些变量加入到这个等式中。

或者考虑这些变化,以使网站上的指标准确无误。

 // JavaScript Document
var today = new Date(); 
var dayNow = today.getDay();
var hourNow = today.getHours();
var shopStatus


if (dayNow = 0) {
    shopStatus = 'See you on Monday';
}

// Regular Hours for Monday - Thursday  
// night time message 
else if  (hourNow > 21) {
        dayNow < 4
        shopStatus = 'Open at 7am';
}   
// closed hours message
else if (hourNow >18 ) {
        dayNow < 5
        shopStatus = '<p class="red">Closed Now</p>';
}   
// 6:30pm closing soon message
else if (hourNow > 17.5) {
        dayNow < 4
        shopStatus = 'Closing soon';
}   
//  Mon - Thurs. open hours message
else if (hourNow > 7 ) {
        dayNow < 4
        shopStatus = 'Open Now';
}   
// 6:30am opening soon
else if (hourNow > 6.5) {
        dayNow < 4
        shopStatus = 'Opening Soon';
}
document.write('<p>' + shopStatus + '</p>');

3 个答案:

答案 0 :(得分:1)

您可以使用&&||为AND创建相当复杂的条件,或者:

if (dayNow == 0) {
  shopStatus = 'See you on Monday'; // Sunday - closed
}
else if ( dayNow == 1 )  && 
   ( hourNow > openingHour ) && 
   ( hourNow < closeToClose ) { // Monday opening hours
  shopStatus = 'message' ;
// next tests... 

就个人而言,我更喜欢另一种方法。使用简单的商店状态索引创建数组。噢 - 怎么回事......

var shopStatus = ["Open", "Closed", "Closing soon!"];

用简单(不那么简单)的测试检查状态:

if (
    ( dayNow < 6 ) && ( dayNow > 1) // it's not Sun/Mon/Sat
    && ( hourNow < 17.5 ) && ( hourNow > 7 )
   ) // and it's open, OR
   || (
    ( dayNow == 1 ) || ( dayNow == 6 )
   ) { // it's Monday or Saturday
  document.write('<p>' + shopStatus[0] + '</p>');
    // message is "Open"
}
else if  // I think you are there

记下具有特定数组索引的每个状态的所有条件。如果需要,此解决方案非常干净,以后很容易翻译。我建议对所有条件进行评论以便于调试。

有关详细信息,请参阅MDN

答案 1 :(得分:0)

如果你想要多个条件,你应该使用

AND

&amp;&amp;

或者

OR

||

如果(condition1 == 5&amp;&amp; condition2&gt; 12){ }

请记住,在比较条件时,应始终使用==而不是=

答案 2 :(得分:0)

好吧,所以我想出来了,至少它现在有用了,我试着在我的电脑上切换时钟以便在奇数天(星期五和星期六)进行测试。让我知道你的想法;但这适用于我正在使用它的网站。

var today = new Date(); 
var dayNow = today.getDay();
var hourNow = today.getHours();
var shopStatus

/*dayNow determins if it is Friday, Saturday or Sunday */

if (dayNow == 0 || dayNow == 6 && hourNow > 16) {
  shopStatus = '<p class="orange">See you on Monday'; // Sunday - closed
}
// night message
else if  (hourNow > 21 && dayNow < 5 && dayNow > 0) {
        shopStatus = 'Open at 7am';
}
// Late Friday night message
else if ( dayNow == 5 && hourNow > 21) {
        shopStatus = '<p class="red">Open at 9am</p>';
}
// insert right after closed message
else if ( dayNow == 5 && hourNow > 14) {
        shopStatus = '<p class="red">Closed Now</p>';
}
/*Regular Hours for Monday - Thursday   
closed hours */

else if (hourNow >18 ) {
        shopStatus = '<p class="red">Closed Now</p>';
}   
// 6:30pm closing soon
else if (hourNow > 17.5) {
        shopStatus = 'Closing soon';
}   
//  Mon - Thurs. open hours
else if (hourNow > 7 ) {
        shopStatus = 'Open Now';
}   
// 6:30am opening soon
else if (hourNow > 6.5) {
        shopStatus = 'Opening Soon';
}

document.write('<p>' + shopStatus + ' *</p>');