如何在Bootstrap日期选择器中添加最大日期

12

我需要为日期选择器添加maxdate,我正在使用这个插件

cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.min.js

这是我的日期选择器代码:

.datepicker({
    autoclose: true,
    todayHighlight: true,
    format: 'mm/dd/yyyy',
    minDate:  new Date(),                                            
    maxDate: +42,
    startDate: indianTimeZoneVal
})

但是maxdate无法使用,我需要从当前日期开始往后推算42天。


Translated:

但是maxdate无法使用,我需要从当前日期开始往后推算42天。

5个回答

19

不确定bootstrap-datepicker是否具有minDate和maxDate选项。

但是它肯定有startDate和endDate选项。试试这个:


.datepicker({
 autoclose: true,
 todayHighlight: true,
 format: 'mm/dd/yyyy',
 startDate: new Date(),
 endDate: new Date(new Date().setDate(new Date().getDate() + 5))
})

这里是示例fiddler,供您参考。

希望对您有所帮助。


当然,兄弟 - 再次感谢您 - kumar swamy
有没有办法让一个日期选择器设置另一个的开始日期?一直在寻找这个功能。 - Fuseteam

5
$('#element').datepicker({
autoclose: true,
todayHighlight: true,
format: 'mm/dd/yyyy',
startDate:  new Date(),                                            
endDate: new Date(new Date().setDate(new Date().getDate() + 42)),
minDate:  new Date(),                                            
maxDate: new Date(new Date().setDate(new Date().getDate() + 42))
})

0
在 ES6 中,我们可以使用 JavaScript 来实现它。
currentDate= new Date(new Date().getTime() + 86400000).toISOString().substring(0, 10);
// convert to substring because datepicker format
//since I am using angular but it's fine with native JS
                <input type="date" id="dateSlot" class="form-control app-inputfield" [min]="currentDate" placeholder="mm/d/year" [(ngModel)]="serviceDate" [ngModelOptions]="{standalone: true}">

0

你需要在引号之间写出最大日期(endDate):

endDate: '+42d'

0
.datepicker(
    {
        format: 'mm/dd/yyyy',
        autoclose: true,
        todayHighlight: true,
        endDate: '0', 
    }
);

2
虽然这段代码片段可能是解决方案,但包括解释真的有助于提高您的帖子质量。请记住,您正在回答未来读者的问题,而这些人可能不知道您的代码建议的原因。 - Neo Anderson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接