javascriptdatejs

How to compare days with getDay()


I'm trying to start a simple booking application in javascript and I have some troubles in the very first step. I want to exclude saturday and sunday from my array.

here is my script (I use datejs):

var today = Date.today(),
    dateFrom = (today.getDay() !== 0) ? today : today.add(1).day(),
    dateTo = Date.today().addWeeks(1),
    dates = [];

while(dateFrom <= dateTo) {
    if(dateFrom.getDay() != 0 || dateFrom.getDay() != 6){
        dates.push(dateFrom.toString('dd/MM'));
        console.log(dateFrom.getDay());
    } 
    dateFrom.add(1).day();
};
<script src="//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script>

As you can see in the console logs, sunday and saturday are still in the array.

BUT it works if I change my condition like this (I have a problem with the != operator when I have two conditions to test) :

var today = Date.today(),
    dateFrom = (today.getDay() !== 0) ? today : today.add(1).day(),
    dateTo = Date.today().addWeeks(1),
    dates = [];

while(dateFrom <= dateTo) {
    if(dateFrom.getDay() == 0 || dateFrom.getDay() == 6){
    } else {
        dates.push(dateFrom.toString('dd/MM'));
        console.log(dateFrom.getDay());
    }
    dateFrom.add(1).day();
};
<script src="//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script>

I also need to change dateFrom = (today.getDay() !== 0) ? today : today.add(1).day() to set it on the next monday if we are saturday on sunday (but I have the same problem If change the condition for dateFrom = (today.getDay() !== 0 || today.getDay() !== 6) ? today : today.add(1).day()


Solution

  • today.getDay() !== 0 || today.getDay() !== 6
    

    means not sunday OR (||) not saturday, but in this case:

    saturday is not sunday, so it will pass and sunday is not saturday, so it will pass also

    What you want is:

    not sunday AND (&&) not saturday

    today.getDay() !== 0 && today.getDay() !== 6
    

    That's why it's working in the second example, just that you are using the else block. You can modify it to:

    if(dateFrom.getDay() != 0 && dateFrom.getDay() != 6){
        dates.push(dateFrom.toString('dd/MM'));
        console.log(dateFrom.getDay());
    }
    

    About jumping to monday, you have to add 2 for saturday and 1 for sunday:

    dateFrom = today.getDay() === 0 ?
               today.add(1).day() :
               (today.getDay() === 6) ? today.add(2).day() : today);
    

    A simpler method would be to do the following:

    var date = Date.today(),
        dates = [];
    
    while(dates.length < 5) {
      if(date.getDay() !== 0 && date.getDay() != 6) {
        dates.push(date.toString('dd/MM'));
      }
      date = date.add(1).days();
    }
    

    Start from today date onward, fill the array and skip saturday and sunday until you have 5 elements in the array (working days).

    var date = Date.today(),
        dates = [],
        log = '';
    while(dates.length < 5) {
      if(date.getDay() !== 0 && date.getDay() != 6) {
        dates.push(date.toString('dd/MM'));
        log += dates[dates.length - 1] + '\n';
      }
      date = date.add(1).days();
    }
    
    $(function() {
      $('#result').text(log);
    });
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script>
    <pre id="result"></pre>