Search code examples
javascripttimepicker

How to set a time using JavaScript pickatime library


I'm using http://amsul.ca/pickadate.js/time/ library to config an input field as a time picker:

<input type="text" class="pickatime" name="startTime" id="startTime">

Then I started the picker time using:

var pickerStart;
function setStartTime()
{
    pickerStart = $('#startTimeEdit').pickatime({
        interval: 15,
        min: [5,30],
        max: [23,0],
        formatSubmit: 'HH:i',
        hiddenName: true
    });
    pickerStart.set('select', '08:30', { format: 'hh:i' });
}

But I can't find a way to set the time after the picker is initialize.

Is there a way to restart it or set its value?

The documentation is not clear about it.


Solution

  • As per the API, you should first store the picker in a variable

    var picker = $('#startTimeEdit').pickatime().pickatime('picker')
    

    Then you will be able to call methods on this variable. In order to set a particular time you can use below method :

    //setting random date
    picker.set('select', [2015, 3, 20])
    
    //setting current date
    picker.set('select', new Date())
    
    // Using arrays formatted as [HOUR,MINUTE].
    picker.set('select', [3,0])
    
    // Using a string along with the parsing format (defaults to `format` option).
    picker.set('select', '04-30', { format: 'hh-i' })