【问题标题】:DateTimePicker - Disable the times that have already passed in the day (JQuery)DateTimePicker - 禁用当天已经过去的时间(JQuery)
【发布时间】:2015-09-15 20:28:53
【问题描述】:

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

我尝试使用 minTime:0 但这也会禁用sunsequent天的时间。我希望能够选择一个开始和结束时间(例如上午 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>

【问题讨论】:

  • 您可以对插件提供的onShow() 事件处理程序应用限制。
  • 那么容易实现吗?
  • 视情况而定。你肯定需要一些脚本。
  • 不确定是否可以使用此插件

标签: javascript jquery datetime datetimepicker


【解决方案1】:

是的,将 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 });    
    } 
});

【讨论】:

    【解决方案2】:

    如果有人卡住了,试试这个

    $( 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      相关资源
      最近更新 更多