Javascript OR operator not working in if statement

后端 未结 6 1428
礼貌的吻别
礼貌的吻别 2020-12-11 23:38

I\'m trying to get this Javascript to do something if the day of the week matches any of the days listed in my statement, as well as restricting it to between 17:00 and 19:0

6条回答
  •  执笔经年
    2020-12-12 00:35

    Just for the sake of posting another possibility, if you ever will have a dynamic input you may want to use an array and use indexOf to check whether the day exists in the list:

    var d = new Date();
    var dayOfWeek = d.getDay(); // 0 = Sunday
    var hour = d.getHours();
    
    if ( [4,5,6].indexOf(dayOfWeek) > -1 && hour >= 17 && hour < 19 ){
        // do stuff
      } else {
        // do other stuff 
    }
    

    https://jsfiddle.net/hnzzfnot/1/

提交回复
热议问题