how to restrict bootstrap date picker from future date

后端 未结 10 1633
无人共我
无人共我 2020-12-09 08:41

I want to restrict the date picker of bootstrap from taking future date.I just want to enable the dates up to today only.How can i achieve this.

Here is my code

相关标签:
10条回答
  • 2020-12-09 09:11
    $(document).ready(function(){
        $(".datepicker").datepicker({
            endDate:'today'
        });
    });
    

    Adding this to your js user can only select date upto today's date and user cannot enter custom date value manually to the input element. If user enters a custom value then it will automatically change to the current date

    0 讨论(0)
  • 2020-12-09 09:13

    Try this,

    $(function () {
        $('.datepicker').datepicker({
            format: 'mm-dd-yyyy',
            endDate: '+0d',
            autoclose: true
        });
    });
    

    I used date picker from Git

    Demo

    0 讨论(0)
  • 2020-12-09 09:18

    Due to issue in plugin it is getting happened use this datepicker.js

    http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.2.0/js/bootstrap-datepicker.js

    0 讨论(0)
  • 2020-12-09 09:20

    Non of the above solutions work for me. After alot of searching and debugging finally I fix it. I am sharing my solution below, If someone like me facing the same problem can try the following solution.

    var now = new Date();
    $('[name=depdate]').datepicker({
        format: 'yyyy-mm-dd',
        onRender: function(date) {
            if(date.valueOf() > now.addDays(15).valueOf()) {
                return 'disabled';
            } else if(date.valueOf() < now.valueOf()) {
                return 'disabled';
            }
        }
    })
    .on('changeDate', function(){
        $(this).datepicker('hide');
    }).data('datepicker');
    
    0 讨论(0)
  • 2020-12-09 09:22
    $(function(){
        $('.datepicker').datepicker({
            format: 'mm-dd-yyyy',
            onRender:function(date){
                return date.valueOf() > FromEndDate.valueOf()?'disabled':'';
            }
         });
    });
    
    0 讨论(0)
  • 2020-12-09 09:24

    You can do it using the option endDate. All the dates after the date specified in endDate option are disabled. If you want to disable dates after today use this in your input tag:

    <input type="text" data-provide="datepicker" data-date-end-date="0d">
    

    Source: https://bootstrap-datepicker.readthedocs.io/en/stable/options.html#id5

    0 讨论(0)
提交回复
热议问题