Home > other >  Check if day of week is between Monday and sunday in nodejs
Check if day of week is between Monday and sunday in nodejs

Time:10-18

i am working on a food application just like foodpanda, i am facing an issue that a restaurant start day is Monday and end day is Friday, i am trying to restrict users do not order(Schedule) Saturday and Sunday(or any other day when the restaurant will be out of service. i also have values of start day and end day in my db.

CodePudding user response:

You can get the current day with

let x = new Date()
x.getDay()

If you want the day to be a string instead of a number (0 to 6 in this case) use

var options = { weekday: 'long'};
console.log(new Intl.DateTimeFormat('en-US', options).format(x));

Now use the first one for the following solution:

let x = new Date().getDay()
if (x < 5) return // cancel order here.

CodePudding user response:

It seems to be a matter of evaluating whether the day is inside a range or not (number inside a range of numbers).

Assuming you're using days from monday to sunday as 0 to 6, it should be like this:

// Evaluating if a day is inside working shift ('from' starting shift, 'to' ending shift) 
function inRange(day, from, to) {
  if(day >= from && day <= to)
    return true;

  return false;
}
  • Related