jquery - timepicker 禁用当前时间之前的时间

标签 jquery timepicker

我正在使用这个 jquery 时间选择器:http://jonthornton.github.io/jquery-timepicker/

它提供了一个开箱即用的输入下拉列表,其中小时数按 30 分钟递增,这正是我所需要的。

如何在时间选择器中禁用当前时间之前的所有时间?

例如,如果当前时间为下午 04:10,则应禁用 04:30 之前的所有值。

API 仅提供此信息:

disableTimeRanges Disable selection of certain time ranges. Input is an array of time pairs, like `[['3:00am', '4:30am'], ['5:00pm', '8:00pm']]. The start of the interval will be disabled but the end won't. default: []

现有代码:

 var timeOptions = {
        'timeFormat': 'h:i A'
    };
    $('#pickTime').timepicker(timeOptions);

最佳答案

试试这个:

function getCurrentTime(date) {
    var hours = date.getHours(),
        minutes = date.getMinutes(),
        ampm = hours >= 12 ? 'pm' : 'am';

  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;

  return hours + ':' + minutes + ' ' + ampm;
}

var timeOptions = {
        'timeFormat': 'h:i A',
        'disableTimeRanges': [['12am', getCurrentTime(new Date())]]
    };

或者你可以使用最短时间:

var timeOptions = {
    'timeFormat': 'h:i A',
    'minTime': getCurrentTime(new Date())
};

关于jquery - timepicker 禁用当前时间之前的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30167823/

相关文章:

javascript - 在 Google Maps Places API 中更改默认文本 "Enter a Location"

jQuery - 如何为这种特殊情况替换 $(element).nextAll().remove()

Flutter - TextFormField 上的 Time.now()

jquery - 如何使用 jquery timepicker 从输入文本框中设置 fullcalendar 的开始时间和结束时间

java - 以编程方式将设置的 TimePicker View 更改为微调模式

jquery - 如何在 jquery 的 .html() 中执行 jquery/javascript 代码

c# - ViewModel 中的类似属性名称未在 Controller 操作方法中绑定(bind)

javascript - 如何从 $.get() 函数内部编辑由 anchor 触发的事件?

javascript - Twitter Bootstrap timepicker defaultTime NaN

java - 在同一个 Activity 中实现两个 TimePickers