关闭或打开JavaScript时如何显示

时间:2019-03-04 15:40:00

标签: javascript

我正在使用https://codepen.io/zeinab92/pen/xwWGWM

中的以下代码

如何编辑javascript,因此打开时间如下:

Monday  6am–1am
Tuesday 6am–1am
Wednesday   6am–1am
Thursday    6am–1am
Friday  6am–1am
Saturday    8am–6pm
Sunday  10am–4pm

我尝试执行以下操作,但是当我访问该网页时,尽管我已经设置了星期几和小时数,但仍然显示“我们已关闭”(星期一)。我感觉else语句可能与之冲突。

任何人都可以帮忙

    var now = new Date();
    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

....
....
....
....
      if ((dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3 || dayOfWeek == 4 || dayOfWeek == 5) && hour >= 6 && hour <= 0) {
        hour = ((hour + 11) % 12 + 1); //i.e. show 1:15 instead of 13:15
        timeDiv.innerHTML = 'it\'s ' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re open!';
        timeDiv.className = 'open';
      } 

      else if ((dayOfWeek == 6) && hour >= 8 && hour <= 18) {
        hour = ((hour + 11) % 12 + 1);
        timeDiv.innerHTML = 'it\'s ' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re open!';
        timeDiv.className = 'open';
      } 

      else if ((dayOfWeek == 0) && hour >= 10 && hour <= 16) {
        hour = ((hour + 11) % 12 + 1);
        timeDiv.innerHTML = 'it\'s ' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re open!';
        timeDiv.className = 'open';
      } 

      else {
        if (hour == 0 || hour > 12) {
          hour = ((hour + 11) % 12 + 1); //i.e. show 1:15 instead of 13:15
        }
        timeDiv.innerHTML = 'It\'s ' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re closed!';
        timeDiv.className = 'closed';
      }
    };
  ....
...
...
...

2 个答案:

答案 0 :(得分:0)

如果您检查小时数是> = 6和<= 0,则您的第一个if错误。

使用下一个

if( ([1,2,3,4,5].indexOf(dayOfWeek) >= 0) && 
   ((hour >= 6 && hour <= 23) || hour == 0) ) { ... }

在if语句之后,您还具有<= 18之类的内容。这还将包括18:30h,直到18:59为止所有内容

答案 1 :(得分:0)

我同意上面的回答和评论,这实际上应该是服务器端的计算。

但是,问题出在第一个if语句的代码中,尤其是本节:

hour >= 6 && hour <= 0

小时实际上不可能大于或等于6,但也小于或等于0,因此完全忽略了该部分并移至最后的else语句。

我对此的建议是将本节更改为:

((hour >= && hour <= 23) || hour == 0)