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

如何基于现有jQuery UI Timepicker配置实现选当前时间并隐藏当日已过时点?

Hey there! Let's walk through how to make your jQuery UI Timepicker meet your two key requirements: auto-selecting the current time, and hiding any times that have already passed today. Here's the step-by-step solution:

1. Hide Past Times by Setting a Dynamic Minimum Time

To ensure only current and future times are visible, we need to set the minTime option to the current time. Since you've enabled forceRoundTime: true with a 15-minute step, the timepicker will automatically round up the current time to the nearest valid interval (e.g., if it's 10:08 AM, it'll set the minimum time to 10:15 AM).

2. Auto-Select the Current Time

We'll add the defaultTime: "now" option to pre-fill the input with the rounded current time, and keep your existing scrollDefault: "now" to ensure the dropdown menu scrolls directly to this time when opened.

Modified Full Initialization Code

// Grab the current date/time to use as our minimum threshold
const currentTime = new Date();

$("#ReminderTime").timepicker({
  "showDuration": true,
  "timeFormat": "g:i A",
  "step": 15,
  "forceRoundTime": true,
  "maxTime": "11:45pm",
  "className": "timepicker-width",
  "scrollDefault": "now",
  // Hide any times before the current moment
  "minTime": currentTime,
  // Pre-fill the input with the rounded current time
  "defaultTime": "now"
});

Key Notes on the Changes:

  • minTime: currentTime: This tells the timepicker to filter out all times that occur before the current moment. The forceRoundTime setting will adjust this to the next valid 15-minute interval if the current time isn't already aligned with your step.
  • defaultTime: "now": This ensures the input field starts with the rounded current time selected, so users don't have to manually choose it. Combined with scrollDefault: "now", the dropdown will automatically focus on the current time when opened.
  • Your existing maxTime: "11:45pm" will still work as expected—any times beyond this will be hidden, even if they fall after the current time.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:42:50