jQuery 如何从未来日期限制引导日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20964218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:22:34  来源:igfitidea点击:

how to restrict bootstrap date picker from future date

jquerytwitter-bootstrapdatepickerbootstrap-datepicker

提问by user2142786

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

任何机构都可以对此提供帮助吗

回答by TJ Nicolaides

Here is a demo of a working solution:

这是一个工作解决方案的演示:

http://jsfiddle.net/tjnicolaides/cjp7y/

http://jsfiddle.net/tjnicolaides/cjp7y/

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

edit: the version of Bootstrap Datepicker that supports startDate and endDate is here: https://github.com/eternicode/bootstrap-datepicker

编辑:支持 startDate 和 endDate 的 Bootstrap Datepicker 版本在这里:https: //github.com/eternicode/bootstrap-datepicker

The original project, which is the current top search result for the plugin, does not have that feature at this time: http://www.eyecon.ro/bootstrap-datepicker/

原始项目,即插件当前的热门搜索结果,目前没有该功能:http: //www.eyecon.ro/bootstrap-datepicker/

回答by Pranav C Balan

Set endDateproperty to +0d

endDate属性设置为+0d

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

FIDDLE

小提琴

回答by Nithya

This code worked for me..

这段代码对我有用..

$('#txtTo').datepicker({
    dateFormat: "dd/MM/yy",
    changeMonth: true,
    changeYear: true,
    ignoreReadonly: true,
    maxDate: 'now'
});

回答by KATJ Srinath

$(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

将此添加到您的 js 用户只能选择截至今天的日期,并且用户无法手动将自定义日期值输入到 input 元素中。如果用户输入自定义值,则它将自动更改为当前日期

回答by Rohan Kumar

Try this,

尝试这个,

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

I used date pickerfrom Git

我使用的日期选择器Git

Demo

演示

回答by user2142786

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

由于插件中的问题,它正在发生使用这个 datepicker.js

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

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

回答by Rachel

$(function(){
    $('.datepicker').datepicker({
        format: 'mm-dd-yyyy',
        onRender:function(date){
            return date.valueOf() > FromEndDate.valueOf()?'disabled':'';
        }
     });
});

回答by MasoodUrRehman

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');

回答by Mark O Brian

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:

您可以使用选项 endDate 来完成。在 endDate 选项中指定的日期之后的所有日期都被禁用。如果要禁用今天之后的日期,请在输入标签中使用它:

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

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

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

回答by Mohan kumar

Try this:

尝试这个:

var date=new Date();
$('#datetimepicker').datetimepicker({
    format: 'MM/dd/yyyy',
    language: 'en',
    startDate : new Date('2015-01-01'),
    endDate :  date
});