1 回答
TA贡献1824条经验 获得超5个赞
问题是你总是返回excluded道具excludeTimes。只有选择的日期是日期才需要返回时间。类似的东西应该工作。(const [value, setValue] = useState();也通过添加)。
<DatePickerField
filterDate={isSunday}
minTime={new Date(new Date().setHours(8, 0, 0))}
maxTime={new Date(new Date().setHours(17, 0, 0))}
excludeTimes={excluded.map((exclude) => {
const excDate = new Date(exclude.date);
if (
value &&
excDate.getDate() === value.getDate() &&
excDate.getFullYear() === value.getFullYear() &&
excDate.getMonth() === value.getMonth()
) {
return new Date(new Date().setHours(exclude.time, 0, 0));
}
return null;
})}
timeIntervals={60}
maxDate={addDays(new Date(), 3)}
dateFormat="d/M/Y HH:mm"
timeFormat="HH:mm"
name="time-to-contact"
label="Required Date *"
onChange={(e) => {
setValue(new Date(e));
}}
selected={value}
/>
此外,您的日期格式不适合 JS,应该是:
const excluded = [
{
time: "12",
date: "09/28/2020"
},
{
time: "13",
date: "10/03/2020"
}
];
这是一个工作示例的链接: https: //codesandbox.io/s/competent-smoke-4nv1e ?file=/src/App.js
要为特定日期(比如星期一)添加排除时间,您可以使用以下命令:
const excluded = [
{
time: "12",
day: 1
},
]
并将道具设置为:
excludeTimes={excluded.map((exclude) => {
if (value && value.getDay() === exclude.day) {
return new Date(new Date().setHours(exclude.time, 0, 0));
}
return null;
})}
添加回答
举报