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

Moment.js日期选择限制:需实现禁用当前日期后5天的日期选择

Solution: Restrict Date Selection to ±5 Days from Current Date

Got it, let's tweak your code to add the future date restriction while keeping the existing past date limit intact.

Breakdown of the Original Code

Your original code uses subtract() to set the earliest allowed date (5 days before today):

const day = 5;
const checkedDate = Datetime.moment().subtract(day, 'day');

This stops users from picking dates older than 5 days ago. Now we just need to mirror this logic to block dates too far in the future.

Adjusted Code for Dual Restriction

To lock out dates 5 days after today, we'll use Moment.js's add() method to calculate the latest allowed date, then apply both limits to your date picker's disabled logic:

const day = 5;
// Earliest allowed date: 5 days before today
const minAllowedDate = Datetime.moment().subtract(day, 'day');
// Latest allowed date: 5 days after today
const maxAllowedDate = Datetime.moment().add(day, 'day');

Applying the Restrictions to Your Date Picker

Update your date picker's disabled date check to validate against both boundaries. Here's a common implementation (works with most UI libraries that accept a disabledDate callback):

// Example disabled date logic (adjust for your specific date picker)
function disabledDate(time) {
  // Disable dates that are earlier than minAllowedDate OR later than maxAllowedDate
  return time.getTime() < minAllowedDate.valueOf() || time.getTime() > maxAllowedDate.valueOf();
}

How It Works

  • If today is 2019/05/10, minAllowedDate becomes 2019/05/05 and maxAllowedDate becomes 2019/05/15.
  • Any date before 2019/05/05 or after 2019/05/15 will be grayed out and unselectable, exactly matching your requirement.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:22:44