简体   繁体   English

检查nodejs中的星期几是否在星期一和星期日之间

[英]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.

相关问题 我如何获得本周一和周日之间的日期AngularJS - How I can get the date this week between Monday and Sunday AngularJS Javascript:获取前一周的周一和周日 - Javascript: get Monday and Sunday of the previous week 请问如何在javascript中让工作日从星期一而不是星期日开始? - How to make the week days start at monday instead of sunday in javascript please? 从星期一而不是星期日开始 antd RangePicker (datepicker) 周 - start antd RangePicker (datepicker) week from monday instead of sunday 在 Javascript 中获取周一开始和周日结束的一周开始和结束 - Get start and end of week with the start on Monday and end on Sunday in Javascript Unix时间戳和新西兰时区中上周的星期一和星期日 - Monday and Sunday of previous week in Unix timestamp AND in New Zealand timezone 从星期日开始工作日 - javascript - start week day from sunday - javascript 一周的第一天-Date.js中的星期一 - First day of week - Monday in Date.js 使用当前日期生成一周的所有日期,还显示星期一的星期开始日期和星期日的周末 - Generate all dates of a week using current date also show week starting date on Monday and weekends on Sunday 获取本周的开始日期和结束日期(每周从星期一开始,以星期日结束) - Get start date and end date of current week (week start from monday and end with sunday )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM