You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-day-picker:使用numberOfMonths时如何显示上月与当月

修改react-day-picker显示上月和当月

要实现显示上月和当月,只需给DayPicker组件添加fromMonth属性,指定起始月份为上个月的第一天即可:

  1. 先计算上个月的起始日期:
const lastMonth = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1);
  1. 修改组件代码,传入fromMonth:
<DayPicker
  mode="range"
  selected={selectedRange}
  onSelect={handleRangeSelect}
  numberOfMonths={2}
  fromMonth={lastMonth}
/>

这样组件就会以上个月为起始,连续展示两个月(上月+当月),替代默认的当月+下月组合。

内容的提问来源于stack exchange,提问作者Hazel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 01:24:13