时间选择器禁用当前时间之前的时间

timepicker disable time before current time

我正在使用这个 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);

尝试像这样使用 minDate 属性:

$(function () {
    $("#date").datepicker({ minDate: 0 });
});

试试这个:

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())
};