1

我有一个来自 react-day-picker 插件的 DayPickerInput 元素,我不知道如何从当天开始禁用一个月(31 天)后的所有天。请问有什么帮助吗?

谢谢。

4

1 回答 1

3

文档可能更清晰。这应该为你做:

<DayPickerInput
    value={moment(minDate).format('YYYY-MM-DD')}
    dayPickerProps={{
        disabledDays: {
            after: new Date(2018, 3, 20),
        },
    }}
    onDayChange={day => console.log(day)}
/>

将新的 Date(y, m, d) 替换为您的日期。

[根据我的评论编辑]

并非所有月份都是 31 天,如果您真的想在一个月的第一天加上 31 天:

来源:将日期添加到 Date 对象

var myDate = new Date();
myDate.setDate(myDate.getDate() + AddDaysHere);

其中“AddDaysHere”为 31。

如果您只是想确保无法选择下个月的日期,您可以:

// There is probably a billion better ways to get the next available month, this is just basic
let currentMonth = 2;
let nextMonth = currentMonth + 1;
if (nextMonth > 11) { nextMonth = 0;} // I believe javascript months start at 0.

Date(2018, nextMonth, 1)

快乐编码!

于 2018-10-17T16:49:51.770 回答