Set min and max date on react day picker

冷暖自知 提交于 2019-12-10 15:33:24

问题


How do i set maximum or minimum date? Like for instance, i want to limit my daypicker only for the last 2 month (min date) until today (max date). So the user can't choose tomorrow's date. Thanks

http://react-day-picker.js.org/docs/


回答1:


You need to use the disabledDays property. It can be passed a set of modifiers as detailed at http://react-day-picker.js.org/docs/modifiers

The following should do what you need:

var twoMonthsAgo = new Date();
twoMonthsAgo.setMonth(twoMonthsAgo.getMonth() - 2);

<DayPicker disabledDays={
{ 
    before: twoMonthsAgo, 
    after: new Date()
}} />



回答2:


From the docs, React Day Picker Modifiers, it looks like you can pass a prop of fromMonth, which you can calculate to be the month two months before today's date. You can also pass a disabledDays prop that will disable days according to your parameters.

const lastMonth = new Date();
lastMonth.setMonth(lastMonth.getMonth() - 2);
<DayPicker
  fromMonth={lastMonth} 
  disabledDays={{ after: today }}
/>

You may need to tweak that a bit, but it should show you where to go from.



来源:https://stackoverflow.com/questions/45826076/set-min-and-max-date-on-react-day-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!