Search code examples
javascriptjquerytimepickerjquery-ui-timepicker

Setting max time in jQuery Timepicker


Below is the Current JQuery for My Time Pickers. If time is picked from #from field it set max time of #to to selected time.

If I picked 1am from #from the max time that can be pick up on #to is 1am.

var osmaxtime = '20';
$('#from').timepicker({ 'minTime': '9','maxTime': '8','scrollDefaultNow': true,'step': 60 });
$('#from').on('changeTime', function() {
    var sFrom=$(this).val();
    $('#to').timepicker({ 'minTime': '9','maxTime':sFrom,'scrollDefaultNow': true,'step': 60 });
});
$('#to').timepicker({ 'minTime': '9','maxTime': osmaxtime,'scrollDefaultNow': true,'step': 60 });

Now I want to increase the max time for #to Field according to #from field.

e.g., if I pick up 1am from #from the max time for #to is 4am (+4 Hours)

$('#from').on('changeTime', function() {
        var sFrom=$(this).val();
        var sFromPuls=sFrom+4;// Here is the problem because the value of sFrom is in String or something else.
        $('#to').timepicker({ 'minTime': '9','maxTime':sFrom,'scrollDefaultNow': true,'step': 60 });
    });

Solution

  • My Problem was solved by following code -

    $('#from').timepicker({ 'minTime': 8,'maxTime': 20,'scrollDefaultNow': true,'step': 60 });
        $('#from').on('change', function() {
            var fromTime=$(this).val();
            var hrs = Number(fromTime.match(/^(\d+)/)[1]);
            var format = fromTime.match(/00(.*)$/)[1];
            if (format == "pm" && hrs < 12) hrs = hrs + 12;
            if (format == "am" && hrs == 12) hrs = hrs - 12;
            var hours = (hrs+4).toString();         
            var sFromPuls= (parseInt(osSeletecdTime)+4).toString();
            $('#to').timepicker({ 'minTime': fromTime,'maxTime':hours ,'scrollDefaultNow': true,'step': 60 });
        });