首页 > 解决方案 > 如何在 React js 中禁用当前和过去一周的日期?

问题描述

我在反应 js 中使用日历。我正在尝试禁用整个本周。现在,我可以设置 min date min={new Date(2019, 7, 29)} 所以直到今天过去的日期都被禁用。但根据要求,如果一周已经开始,那么用户不能选择整个当前周。即今天的日期是 2019 年 7 月 29 日,所以应该禁用 2019 年 8 月 4 日之前的日期。我的一周从星期一开始,到星期日结束。如果它的任何功能都可以用来实现这个功能,我也在使用 lodash 库。TIA。

标签: javascriptreactjs

解决方案


检查当前日期并相应地更改您的开始日期。

let minDate = newDate();
const today = new Date();
if (today.getDay() === 0) { // note: this returns 0-7 with 0 being sunday 
  // its sunday, set min date to tomorrow(beginning of your week)
  minDate = minDate.setDate(today.getDate()+1);
} else { // now we know its monday - sat
  const offset = 8 - today.getDay(); // this is the number of days till the next week.
  minDate = minDate.setDate(today.getDate()+offset);
}

推荐阅读