React-day-picker:使用numberOfMonths时如何显示上月与当月
修改react-day-picker显示上月和当月
要实现显示上月和当月,只需给DayPicker组件添加fromMonth属性,指定起始月份为上个月的第一天即可:
- 先计算上个月的起始日期:
const lastMonth = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1);
- 修改组件代码,传入
fromMonth:
<DayPicker mode="range" selected={selectedRange} onSelect={handleRangeSelect} numberOfMonths={2} fromMonth={lastMonth} />
这样组件就会以上个月为起始,连续展示两个月(上月+当月),替代默认的当月+下月组合。
内容的提问来源于stack exchange,提问作者Hazel
相关产品推荐
相关产品推荐

