Why timepicker can not be downgraded when choosing

2019-08-24 06:03发布

My html code like this :

<div class="container">
    <div class="row">
        <div class='col-sm-6'>
            <div class="form-group">
                <input type='text' class="form-control" id='datepicker' data-date-format="DD-MM-YYYY" />
            </div>
        </div>

        <div class='col-sm-6'>
            <div class="form-group">
                <input type='text' class="form-control" id='timepicker' data-date-format="HH:mm" />
            </div>
        </div>
    </div>
</div>
<button id="btnSubmit">
    Submit
</button>

My JavaScript code like this:

$(function () {    
    $('#datepicker').datetimepicker();

    $('#timepicker').datetimepicker({
        minDate: moment().startOf('minute').add(300, 'm'),
    });
});
$("#btnSubmit").click(function() {
    value = document.getElementById('datetimepicker').value;
    console.log(value)
});

Demo like this: https://jsfiddle.net/8jpmkcr5/89/

If I click the timepicker and click the decrement hour icon, it does not work. I know it happens because this code : minDate: moment().startOf('minute').add(300, 'm'). I want the code to work only on this day. Other than today the code does not work

How can I do it?

2条回答
欢心
2楼-- · 2019-08-24 06:44

Your #datepicker and #timepicker are unrelated, they are fully independent, is up to you to relate them. You can dinamically set minDate of #timepicker using minDate function.

You can add a listner for dp.change event and enable or disable the minDate for the #timepicker chceking if the selected day is current day (using momentjs isSame).

You have to add minDate option also to #datepicker if you want to disable past dates, as you stated in the comments.

Here a working sample:

$(function () {    
  $('#datepicker').datetimepicker({
    minDate: moment().startOf('day')
  }).on('dp.change', function(e){
    if( e.date && e.date.isSame(moment(), 'd') ){
      var min = moment().startOf('minute').add(300, 'm');
      $('#timepicker').data("DateTimePicker").minDate(min);
    } else {
      $('#timepicker').data("DateTimePicker").minDate(false);
    }
  });
  
  $('#timepicker').datetimepicker({
    minDate: moment().startOf('minute').add(300, 'm'),
  });
});


$("#btnSubmit").click(function() {
   value = document.getElementById('datepicker').value;
   console.log(value)
   valueTime = document.getElementById('timepicker').value;
   console.log(valueTime)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>

<div class="container">
    <div class="row">
        <div class='col-sm-6'>
            <div class="form-group">
                    <input type='text' class="form-control" id='datepicker' data-date-format="DD-MM-YYYY" />
            </div>
        </div>
        
        <div class='col-sm-6'>
            <div class="form-group">
                <input type='text' class="form-control" id='timepicker' data-date-format="HH:mm" />
            </div>
        </div>
    </div>
</div>

<button id="btnSubmit">
    Submit
</button>

Please note that, as stated before, #datepicker and #timepicker are unrelated, so with the #timepicker you are simply selecting a time, that defaults to current day (no automatic relationship with the selected day using #datepicker).

查看更多
冷血范
3楼-- · 2019-08-24 06:46

If you want to limit the input to today only, why are you trying to set a limit on the time picker? You should be setting the limit on the date picker

查看更多
登录 后发表回答