DateTimePicker - 禁用当天已经过的时间(JQuery)

时间:2015-09-15 20:28:54

标签: javascript jquery datetime datetimepicker

我目前正在将XDSoft DateTimePicker(http://xdsoft.net/jqplugins/datetimepicker/)JQuery集成到我的网站中,并希望禁用已经过去的时间。目前,如果我今天选择,那么我仍然可以选择已经过去的时间。

我尝试使用minTime:0但这也禁用了后续几天的时间。我希望能够选择开始和结束时间(例如上午9点至下午5点30分),我可以用最短时间和最长时间进行操作,但我无法禁用今天已经过去的时间。

以下是我的剧本:

<script>
    jQuery('#datetimepicker3').datetimepicker({
        minDate: 0,
        step: 60,
        minTime: '09:00',
        maxTime: '17:30',
        todayButton: true,
        format: 'd.m.Y H:i',
        inline: true,
        lang: 'en'

    });</script>

2 个答案:

答案 0 :(得分:0)

是将 minTime 设置为0也会在后续日期禁用次数。您需要根据日期更改 minTime 选项。如果是今天, minTime 将保持不变,否则 minTime 将设置为 false

$("#element").datetimepicker({ format: 'Y-m-d H:i', minDate: 0, minTime: 0, onSelectDate: function(ct) {
        var dtob = new Date(),
            current_date = dtob.getDate(),
            current_month = dtob.getMonth() + 1,
            current_year = dtob.getFullYear();

        // today's date 
        var full_date = current_year + '-' +
                        ( current_month < 10 ? '0' + current_month : current_month ) + '-' + 
                        ( current_date < 10 ? '0' + current_date : current_date );

        // compare today's date to the date chosen
        if(ct.dateFormat('Y-m-d') == full_date)
            this.setOptions({ minTime: 0 });
        else 
            this.setOptions({ minTime: false });    
    } 
});

答案 1 :(得分:0)

如果有人被卡住,请尝试

$( function() {
var checkPastTime = function(inputDateTime) {
  if (typeof(inputDateTime) != "undefined" && inputDateTime !== null) {
      var current = new Date();
      if (inputDateTime.getFullYear() < current.getFullYear()) {
          $('.datetimepicker').datetimepicker('reset');
          alert("Sorry! Past date time not allow.");
      } else if ((inputDateTime.getFullYear() == current.getFullYear()) && (inputDateTime.getMonth() < current.getMonth())) {
          $('.datetimepicker').datetimepicker('reset');
          alert("Sorry! Past date time not allow.");
      }
      if (inputDateTime.getDate() == current.getDate()) {
          if (inputDateTime.getHours() < current.getHours()) {
              $('.datetimepicker').datetimepicker('reset');
          }
          this.setOptions({
              minTime: current.getHours() + ':00' //here pass current time hour
          });
      } else {
          this.setOptions({
              minTime: false
          });
      }
  }
};
var currentYear = new Date();
$('.datetimepicker').datetimepicker({
    format:'Y-m-d H:i',
    minDate : 0,
    yearStart : currentYear.getFullYear(), // Start value for current Year selector
    onChangeDateTime:checkPastTime,
    onShow:checkPastTime
});

});

信用:

  

https://www.thecodedeveloper.com/disable-past-date-time/