Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CookieMonster's avatar

restrict timepicker based on today date?

I am using jQuery for both datepicker and timepicker. I would like to restrict the time(currently it's 9am-5.00pm) based on whether if the date chosen is today and current time as well. For instance, if I select today date's (9th July) at around 1.00pm then the timepicker should restrict to 2.00pm-5.00pm since it wouldn't make sense for 9.00am-1.00pm.

My jquery:


     $( "#pickupDate" ).datepicker({ 
          dateFormat: 'dd-mm-yy',
          minDate : 0,
          onSelect: function (date) {
            var date2 = $('#pickupDate').datepicker('getDate');
            date2.setDate(date2.getDate() + 1);
            //sets minDate to pick up date + 1
            $('#deliveryDate').datepicker('option', 'minDate', date2);

            // check if current date is today
            //set timepicker to be later range of time
            var current_date = $('#pickupDate').datepicker({);
            console.log(current_date);
           
          }
        });

 $('#pickupTime').timepicker({
            minTime: '9',
            maxTime: '5:00pm',
        });

0 likes
4 replies
CookieMonster's avatar
No errors on the console but I think my logic could be wrong?
anilkumarthakur60's avatar

$('#date').datepicker({
    minDate: 0
});
$('#time').timepicker({
    onHourShow: function( hour ) { 
        var now = new Date();
        // compare selected date with today
        if ( $('#date').val() == $.datepicker.formatDate ( 'mm/dd/yy', now ) ) {
            if ( hour <= now.getHours() ) {
                return false;
            }
        }
        return true;
    }
});​
CookieMonster's avatar

I think the problem is the moment you "click" on the date and also get the current date and time and compared them then it would never match.

Please or to participate in this conversation.