jquerydatetimepicker

Jquery Timepicker set initial time


I am using jquery timepicker I need set the initial time.

let pickip_time = "08:31"
        if(pickip_time){
            pickup_array = pickip_time.split(":");
            $('#pickupTime').timepicker({timeFormat:'HH:mm TT',hour:pickup_array[0],minute:pickup_array[1]});
            //$('#pickupTime').val(pickip_time);
        } else {
            $('#pickupTime').timepicker({timeFormat:'HH:mm TT'});
        }

Above code does not work. I even tried with below code it is not working

$('#pickupTime').timepicker({timeFormat:'HH:mm TT',startTime:pickip_time});

Solution

  • The startTime property is not intended to set initial value, as documentation says it is:

    A Date object or string. The time of the first item in the combobox when the input field is empty. If the input field is not empty the first item will be the next allowed time entry.

    AFAIK the correct way to set initial time on this object is after init, with setTime action:

    // Define initial time
    let pickip_time = "08:31";
    
    // Init timepicker
    $('#pickupTime').timepicker({
        timeFormat: 'HH:mm TT'
    });
    
    // Set time
    $('#pickupTime').timepicker('setTime', pickip_time);