punygod
punygod

Reputation: 307

Checking MomentJS data toString not evaluating properly in if check

I am trying to create a function to create an array of the next two weeks of days that are not Friday, Saturday, or Sunday. moment().isoWeekday().toString() returns a number 1-7 depending on the day. I have an if statement checking to see if each day matches 5, 6, or 7, but they all return true no matter the day. I'm not sure what I'm doing wrong. Please advise.

    for (let i = 1; i < 14; i++) {
      if (moment().add(i, 'days').isoWeekday().toString() !== '5' || 
          moment().add(i, 'days').isoWeekday().toString() !== '6' ||
          moment().add(i, 'days').isoWeekday().toString() !== '7') {
            console.log(moment().add(i, 'days').isoWeekday().toString())
            dayArray.push(moment().add(i, 'days').toString());
      }
    }

Upvotes: 0

Views: 18

Answers (1)

Antonin Riche
Antonin Riche

Reputation: 578

The error seems to come from you condition, you use OR (||) instead AND (&&)

for (let i = 1; i < 14; i++) {
    if (moment().add(i, 'days').isoWeekday().toString() !== '5' &&
        moment().add(i, 'days').isoWeekday().toString() !== '6' &&
        moment().add(i, 'days').isoWeekday().toString() !== '7') {
        console.log(moment().add(i, 'days').isoWeekday().toString())
        dayArray.push(moment().add(i, 'days').toString());
    }
}

Upvotes: 1

Related Questions