简体   繁体   中英

How to add maxdate in bootstrap datepicker

I need to add maxdate for datepicker and i am using this plugin

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

and this is my datepicker code:

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

But maxdate is not working i need current date to next 42 days

Am not sure bootstrap-datepicker has minDate and maxDate.

But for sure it has startDate and endDate instead. Try this,

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

Here is the example fiddler for your reference.

Hope it helps.

$('#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))
})
.datepicker(
    {
        format: 'mm/dd/yyyy',
        autoclose: true,
        todayHighlight: true,
        endDate: '0', 
    }
);

In Es6 we can do it by using js

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}">

你必须在引号之间写下 maxdate (endDate):

endDate: '+42d'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM