-1

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.

Jofre
  • 3,718
  • 1
  • 23
  • 31
  • [Date.prototype.getDay](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) – jabaa Oct 18 '21 at 13:07

2 Answers2

1

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.
Zariaa_
  • 116
  • 1
  • 6
  • 1
    thanks, actually my problem is, a restaurant start day is Wednesday and end day is Friday but user wants to schedule order on Thursday, how i can find that Thursday is a working day or not. my question is how can i check that Thursday is lying in Wednesday between Friday – ADEEL FAROOQ Oct 18 '21 at 13:26
0

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 Sunday to Saturday 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;
}

EDIT: After thinking about it, there are some edge cases we haven't considered.

Let's say you have non working day on Monday (which might be common in some countries). If you try to evaluate it using ranges, you'll find Sunday as a non working day as well (since Sunday is 0, but range starts from Monday, which is 1).

Hence, we need to change the logic to check whether the asked day is non-working or not.

The easiest way to do is by creating a dictionary, mapping each day to a boolean which will say if that day is free or not:

let workingDays = {
  Sunday: true,
  Monday: true,
  Tuesday: true,
  Wednesday: true,
  Thursday: true,
  Friday: true,
  Saturday: true
}

Then, you'll only need to specify the non working days like so

// Working everyday except by Monday
workingDays.Monday = false;

Finally, you can peform verifications to check if the restaurant will be working on the requested day

// Am I working on Monday?
if(workingDays.Monday) {
  // do something
} else { 
  // nope. Come another day.
}

You could also specify the field name as a string which is useful if you want to check using a variable

let requestedDay; // fill this variable somewhere else
...
// is the requested day available?
if(workingDays[requestedDay]) {
  // Do something
}

Finally, since the return value of getDay() is an integer from 0 to 6, you might need to search by index:

// Monday == 1
if( workingDays[Object.keys(workingDays)[1]] )
  // blabla

Or you could wrap it as a function to improve readability

// Wrap function inside workingDays dictionary
workingDays.at = function(n) { return this[Object.keys(this)[n]] };

// Usage
workingDays.at(1); // gives Monday status (boolean)

Final code:

// Creating dictionary
let workingDays = {
  Sunday: true,
  Monday: true,
  Tuesday: true,
  Wednesday: true,
  Thursday: true,
  Friday: true,
  Saturday: true,
  at: function(n) { return this[Object.keys(this)[n]] }
}

// Setting a day as non available
workingDays.Monday = false;
// or
workingDays["Monday"] = false;
// or 
workingDays.at(1) = false;

// Verifying whether a given day is available
if(workingDays[givenDay]) {
  // blabla
}

// or
if(workingDays.at(number)) {
  // blabla
} 
Javier M
  • 194
  • 7