Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable future dates in datepicker?

I have used following code for this but it is not working. please help me to do it.

$(document).ready(function () {

        $('.datepicker').datepicker({
            format: 'mm-dd-yyyy',
            autoclose:true,
            endDate: "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, '');
            }
        });
    });
like image 480
Praddyumna Sangvikar Avatar asked Feb 09 '17 05:02

Praddyumna Sangvikar


3 Answers

You can do this: Datepicker has an option 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>
like image 184
Sorangwala Abbasali Avatar answered Oct 01 '22 01:10

Sorangwala Abbasali


using maxDate:'0' you can acchive this.

Working fiddle

$("#datepicker").datepicker({
  dateFormat: 'yy-mm-dd ',
  maxDate:'0'
});
like image 25
4b0 Avatar answered Sep 30 '22 23:09

4b0


If you want to future date disable from today use today.getFullYear() - 10 in order to disable 10 years before which means disable from 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>
like image 21
abhishek kumar Avatar answered Oct 01 '22 00:10

abhishek kumar