Search code examples
jquerytimepicker

timepicker disable time before current time


I'm using this jquery time picker: http://jonthornton.github.io/jquery-timepicker/

which gives out of the box a dropdown on input, with hours incremented by 30 min, which is exactly what I need.

How can I disable all times before the current time in the time picker?

If, for example, current time is 04:10 PM, then all values before 04:30 should be disabled.

The API only provides this info:

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: []

Existing code:

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

Solution

  • try this:

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

    or you could use min time:

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