'How to exclude holidays like saturday, sunday from date using Javascript?

I've found this code online to return the next working day and exclude weekends.

function nextWorkDay() {
    const date = new Date();
  do {
    date.setDate(date.getDate() + 1);
  } while (!(date.getDay() % 6));
  return date;
}

However, the % 6 part in my understanding excludes Saturdays and Sundays. How can I modify it to only exclude Saturdays?



Solution 1:[1]

I would say always go to next day, and if it's Saturday jump to the next day. The sample working code is attached below.

const days = ["SUN", "MON", "TUS", "WED", "THU", "FRI", "SAT"]

function getNextWorkingDate() {
  let now =  new Date();
  let today = new Date(now.setDate(now.getDate() +  1)) // find next day
  // Sunday = 0, Monday = 1, ... (See below):
  if(today.getDay() == 7) { //  if staturday, jump one more day
    today = today.setDate(today.getDate() +  1)
  }
  return today;
}



const nextWorkigDay = getNextWorkingDate();
console.log(nextWorkigDay);
console.log(days[nextWorkigDay.getDay()]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dipak