how to restrict bootstrap date picker from future

2019-01-24 02:17发布

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

<script>
  var FromEndDate = new Date();

    $(function(){
   $('.datepicker').datepicker({
      format: 'mm-dd-yyyy',
     endDate: FromEndDate, 
    autoclose: true
    });
});
</script>

Can any body help regarding this

9条回答
走好不送
2楼-- · 2019-01-24 02:37

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');
查看更多
3楼-- · 2019-01-24 02:38
$(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

查看更多
虎瘦雄心在
4楼-- · 2019-01-24 02:39

Try this:

var date=new Date();
$('#datetimepicker').datetimepicker({
    format: 'MM/dd/yyyy',
    language: 'en',
    startDate : new Date('2015-01-01'),
    endDate :  date
});
查看更多
冷血范
5楼-- · 2019-01-24 02:43

Set endDate property to +0d

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

FIDDLE

查看更多
戒情不戒烟
6楼-- · 2019-01-24 02:49

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

查看更多
Luminary・发光体
7楼-- · 2019-01-24 02:50

This code worked for me..

$('#txtTo').datepicker({
            dateFormat: "dd/MM/yy",
            changeMonth: true,
            changeYear: true,
         ignoreReadonly: true,
            maxDate: 'now'
        });
    });
查看更多
登录 后发表回答