Why timepicker can not be downgraded when choosing a date other than today?

六月ゝ 毕业季﹏ 提交于 2019-12-12 04:15:46

问题


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?


回答1:


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).




回答2:


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



来源:https://stackoverflow.com/questions/45428366/why-timepicker-can-not-be-downgraded-when-choosing-a-date-other-than-today

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!