javascript - 如何在 React js 中禁用当前和过去一周的日期?
问题描述
我在反应 js 中使用日历。我正在尝试禁用整个本周。现在,我可以设置 min date min={new Date(2019, 7, 29)} 所以直到今天过去的日期都被禁用。但根据要求,如果一周已经开始,那么用户不能选择整个当前周。即今天的日期是 2019 年 7 月 29 日,所以应该禁用 2019 年 8 月 4 日之前的日期。我的一周从星期一开始,到星期日结束。如果它的任何功能都可以用来实现这个功能,我也在使用 lodash 库。TIA。
解决方案
检查当前日期并相应地更改您的开始日期。
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);
}
推荐阅读
- node.js - TypeError:无法读取未定义的“id”属性
- oracle - 如何使用oracle触发器中的计算值更新同一表中的列
- r - 我的 R for 循环只打印最后一个元素
- oop - “聚合”和“组合”关系之间的关系以及“聚合”和“继承”关系之间的关系
- python - 为什么正则表达式中没有 AND 运算符?
- javascript - JQuery Plugin - 突出显示包含某个单词的内容可编辑框
- javascript - React fetch / doPost 没有返回数据
- javascript - IIS 仍然加载旧的 javascript 文件。如何更新或告诉 iis 加载/获取新的 javascript 文件?
- android - 从 AdapterPager 结果中的当前视图中删除最近的项目到异常错误
- android - kotlin flatten 返回多个订单项