BonBoon
BonBoon

Reputation: 107

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

Upvotes: 0

Views: 6359

Answers (3)

BonBoon
BonBoon

Reputation: 107

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

Upvotes: 0

Khaleel
Khaleel

Reputation: 1371

Surround the var with Number to make jQuery recognize it as number to add 4 to it

$('#from').on('changeTime', function() {
  var sFrom=$(this).val();
  var sFromPuls=Number(sFrom)+4; // Surround with Number()
  $('#to').timepicker({ 'minTime': '9',
  'maxTime':sFromPuls, //write correct var name 
  'scrollDefaultNow': true,
  'step': 60 });
});

Upvotes: 1

Earth
Earth

Reputation: 3571

You can refer to onSelect documentation and use it on your timepicker

$('#example').datetimepicker({
    onSelect: function(){
              //Here you can add 4 hours to the start time
        }
});

Upvotes: 0

Related Questions