使用jQuery基于JoinOn日期禁用/限制LeaveOn日期

时间:2018-10-15 07:02:30

标签: jquery datetimepicker

我需要限制/禁用用户,使其根据其 JoinOn 日期的输入选择一个 LeaveOn 日期。示例:如果用户在 JoinOn 日期中选择 2018/10/13 ,则用户将不能选择 LeaveOn 日期作为日期低于 2018年10月13日,即日期不能为 10/10/2018

以下是我的相同代码:

<script>
$(document).ready(function () {
    $('#IsPresent').change(function () {
        if ($(this).is(':checked')) {
            $('#PresentPanel').hide();
            $('#LeaveOn').val("@(DateTime.Today.ToString("MM/dd/yyyy"))");
        }
        else {
            $('#PresentPanel').show();
        }
    });
        $('#JoinOn').datetimepicker(
                      {
                          datepicker: true,
                          timepicker: false,
                          format: 'm/d/Y',
                          step: 30,
                          minDate: new Date(1965, 1 - 1, 1),
                          maxDate: new Date()
                      });
        $('#LeaveOn').datetimepicker(
                      {
                          datepicker: true,
                          timepicker: false,
                          format: 'm/d/Y',
                          step: 30,

                          maxDate: new Date()
                      });
    });

2 个答案:

答案 0 :(得分:0)

假设您已选择日期选择器日期作为事件监听器,则只需添加以下代码即可

var LeaveOnSelectedDate = $('#LeaveOn').datepicker('getDate');
var JoinOnDate = $('#JoinOn').datepicker('getDate');
  JoinOnDate.setHours(0,0,0,0);
  LeaveOnSelectedDate.setHours(0,0,0,0);
  if (LeaveOnSelectedDate <= JoinOnDate) {
    console.log("Date is old or same day!");
  } else {
    console.log("Date is in future!");
  }
  
  
  

答案 1 :(得分:0)

尝试

$(function(e) {
    $('#JoinOn').datetimepicker({
        datepicker: true,
        timepicker: false,
        format: 'd/m/Y',
        maxDate: 0,
        onSelectDate: function(ct, $i) {
            $('#LeaveOn').datetimepicker('setOptions', {
                minDate: ct
            });
        }
    });
    $('#LeaveOn').datetimepicker({
        datepicker: true,
        timepicker: false,
        format: 'd/m/Y',
        maxDate: 0
    });
});

DEMO HERE

希望这会有所帮助