如何在datepicker中禁用将来的日期?

时间:2017-02-09 05:23:15

标签: javascript jquery twitter-bootstrap visual-studio jquery-ui-datepicker

我已经使用了以下代码,但它无效。请帮我做。

Baz* MyAwesomeFunc(){
    return new Foo();
}

Baz* MyAwesomeFunc(int a,int b,int c){
    return new Bar(a,b,c);
}

7 个答案:

答案 0 :(得分:8)

您可以这样做:Datepicker有一个选项maxDate

$(document).ready(function () {
        var today = new Date();
        $('.datepicker').datepicker({
            format: 'mm-dd-yyyy',
            autoclose:true,
            endDate: "today",
            maxDate: today
        }).on('changeDate', function (ev) {
                $(this).datepicker('hide');
            });


        $('.datepicker').keyup(function () {
            if (this.value.match(/[^0-9]/g)) {
                this.value = this.value.replace(/[^0-9^-]/g, '');
            }
        });
    });
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Select Date: <input type="text" class="datepicker"></p>

答案 1 :(得分:0)

使用maxDate:'0'您可以获得此信息。

Working fiddle

$("#datepicker").datepicker({
  dateFormat: 'yy-mm-dd ',
  maxDate:'0'
});

答案 2 :(得分:0)

如果要从今天起禁用日期,请使用today.getFullYear() - 10来禁用10年,这意味着从2008年开始禁用。

<script>
 $(document).ready(function(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear()-10; // change according to year 0 for current
var today1 = mm + '/' + dd + '/' + yyyy;
$("#birthday").datepicker({
     endDate:today1,
    });
});
</script>

答案 3 :(得分:0)

$(document).ready(function () {
        var today = new Date();
        $('.datepicker').datepicker({
            format: 'mm-dd-yyyy',
            autoclose:true,
            endDate: "today",
            maxDate: today
        }).on('changeDate', function (ev) {
                $(this).datepicker('hide');
            });


        $('.datepicker').keyup(function () {
            if (this.value.match(/[^0-9]/g)) {
                this.value = this.value.replace(/[^0-9^-]/g, '');
            }
        });
    });
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Select Date: <input type="text" class="datepicker"></p>

答案 4 :(得分:0)

完全可以这样做:它将从今天起禁用。

$("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true,
        maxDate: "-1d",
        minDate: "-100Y",
        yearRange: "-100:-0"
    });

答案 5 :(得分:0)

如果您使用的是bootstrap-4 date-picker,则可以在输入标签"data-date-end-date" => "0d" i中添加此属性

答案 6 :(得分:0)

var DateIssue = $("#BodyContentPlaceHolder_dat_xxxx_xxxx_True");
              var DateOpts = {
              showButtonPanel: true,
              timepicker:false,
              dateFormat: "yy-mm-dd",
              buttonText: "" ,
              changeMonth: true,
              changeYear: true,
              yearRange: "-100:+50",
              beforeShow: beforeShowDatePicker
};
    DateOpts = {
    maxDate: new Date(),
    showButtonPanel: true,
    timepicker:false,
    dateFormat: "yy-mm-dd",
    buttonText: "",
    changeMonth: true,
    changeYear: true,
    yearRange: "-100:+50",
    beforeShow: beforeShowDatePicker
};
  $(DateIssue).datetimepicker('destroy');
  $(DateIssue).datepicker(DateOpts)