[英]Check if day of week is between Monday and sunday in nodejs
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).我正在开发一个类似于 foodpanda 的食品应用程序,我面临的问题是餐厅的开始日是星期一,结束日是星期五,我试图限制用户不要在星期六和星期日(或任何其他日期)订购(时间表)餐厅将停止服务)。
I also have values of start day and end day in my db.我的数据库中也有开始日和结束日的值。
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如果您希望日期是字符串而不是数字(在本例中为 0 到 6),请使用
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.
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:假设您使用从星期一到星期日的天数为 0 到 6,它应该是这样的:
// 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.