ToDate不应低于FromDate

时间:2017-03-28 10:33:08

标签: javascript jquery twitter-bootstrap datepicker

我有两个日期选择器,一个用于Fromdate,第二个用于Todate。我想ToDate不应该比Fromdate少。我试图解决这个问题,但我无法正确解决这个问题。

$(document).on('focus', '.datepicker', function() {
            // alert();
            $(this).datepicker({
                 format: "mm/dd/yyyy",
				//startDate: '-90d',
				//endDate: '+0d',
                autoclose: true

            });
			
			$(".datepickerInputTO").change(function () {
    var fromDate = $(".datepickerInputFROM").val;
    var toDate = $(".datepickerInputTO").val;
 
    if (toDate <= fromDate) {
        alert("End date should be greater than Start date");
        $(".datepickerInputTO").val = "";
    }
});
        });
<script src="https://code.jquery.com/jquery-1.12.4.min.js"
  integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
  crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css"></script>

<div class="col-md-3">
						<input type="text" placeholder="Select from Date" class="form-control form-control-inline input-medium datepicker datepickerInputFROM">
						</div>
						<div class="col-md-3">
						<input type="text" placeholder="Select to Date" class="form-control form-control-inline input-medium datepicker datepickerInputTO">
						</div>

1 个答案:

答案 0 :(得分:0)

您可以changeDate执行此操作,如果todate更改为更大的日期值,您可以更改fromdate

 $(document).ready(function () {
    $(".datepickerInputFROM").datepicker({
        format: 'dd/mm/yyyy',
        autoclose: 1,
        //startDate: new Date(),
        todayHighlight: false,
        endDate: new Date()
    }).on('changeDate', function (selected) {
        var minDate = new Date(selected.date.valueOf());
        $('.datepickerInputTO').datepicker('setStartDate', minDate);
        $(".datepickerInputTO").val($(".datepickerInputFROM").val());
        $(this).datepicker('hide');
    });

    $(".datepickerInputTO").datepicker({
        format: 'dd/mm/yyyy',
        todayHighlight: true,
        endDate: new Date()
    }).on('changeDate', function (selected) {
        $(this).datepicker('hide');
    });
});

<强> DEMO HERE