否则,JavaScript中的If语句似乎会在Moment.js工作日中导致一些奇怪的行为

否则,JavaScript中的If语句似乎会在Moment.js工作日中导致一些奇怪的行为,javascript,momentjs,Javascript,Momentjs,我正在使用Moment.JS检查当前日期是不是星期天还是星期六。如果不是,则执行一些操作 这是我的密码: let currentDay = moment().weekday(); if(currentDay !== 0 || currentDay !== 6){ doSomeOperation(); } else { console.log("we should get here on a Sunday"); } 这对我来说完全有道理——如果当前日期不是星期天或星期六,那么do

我正在使用Moment.JS检查当前日期是不是星期天还是星期六。如果不是,则执行一些操作

这是我的密码:

let currentDay = moment().weekday();

if(currentDay !== 0 || currentDay !== 6){
     doSomeOperation();
  } else { console.log("we should get here on a Sunday"); }
这对我来说完全有道理——如果当前日期不是星期天或星期六,那么
doSomeOperation()(对于上下文,我将在星期天运行此程序。)

但是,它失败并运行
doSomeOperation()if
块中的code>方法。我已经在每种可能的组合中运行了它,但它仍然失败。然后我决定分别运行它们

if(currentDay !== 0){
         doSomeOperation();
  } else { console.log('you should get here'); }
此操作成功-我进入
else
块。这是毫无意义的——因为如果成功了,那么为什么上面提到的失败呢

我最后改成这样:

if ((currentDay === 0 || currentDay === 6)){
console.log('you should get here');
} else { doSomeOperation() }

这成功了,我打印出“你应该到这里”。如果我反转运算符,则所有检查都通过。我的问题是我做错了什么?

您需要在条件中选择逻辑AND,因为您希望排除这两天

if (currentDay !== 0 && currentDay !== 6) {
    // do some operations
}
要否定该条件,您可以应用


你好@Nina-谢谢你的回复。为什么当我反转运算符时,它们都使用逻辑OR?它不使用equal and OR,因为每个部分有时都是
true
,条件总是
true
。当我在if语句中遇到问题时,我总是尝试将它们转换为问题,看看它们是否有意义。如果我不能,那么代码通常会有问题。或者代码太复杂,需要分解。@MaxBilbow,oyu可以拿一张真相表,用笔和纸做一张支票,什么对你有用。
if (currentDay === 0 || currentDay === 6) {
    // saturday or sunday
} else {
    // other days
    // do some operations
}