I know there is the possibility to disable dates in the ngx-bootsrap Datepicker by using the datesDisabled
property of the bsConfig
.
Anyway, I was wondering if it possible to also to the opposite: allowing only specific dates?
i.e. with a property like
datesEnabled = [ new Date('2019-03-21'), new Date('2018-03-21') ]
The only dates that should still be enabled in the datepicker like this, should then be 2019-03-21
and 2018-03-21
.
I've already tried to use lambda expressions in datesDisabled
, but nothing seemed to work.
UPDATE
Unfortunately there is no property like enabledDates in official documentation
https://valor-software.com/ngx-bootstrap/#/datepicker#dates-disabled
So here is the work around for your need
Working code:
https://stackblitz.com/edit/angular-xxzzex
export class AppComponent implements OnInit{
name = 'Angular';
millisecondPerDay = 24 * 60 * 60 * 1000;
disabledDates = [];
enabledDates = [ new Date('2019-03-15'), new Date('2019-03-17'), new Date('2019-03-11') ]
ngOnInit() {
this.GetDisabledDates(this.enabledDates);
}
GetDisabledDates(excludeDates: Array<Date>)
{
var now = new Date();
var startDate:Date = new Date(now.setFullYear(now.getFullYear() - 1));
var endDate:Date = new Date(now.setFullYear(now.getFullYear() + 2));//change as per your need
console.log(startDate);
console.log(endDate);
this.disabledDates = [];
do{
var found = false;
for(var i=0;i<excludeDates.length;i++)
{
var excludeDate: Date = excludeDates[i];
if(this.IsSameDay(excludeDate,startDate))
{
found = true;
}
}
if(!found)
{
this.disabledDates.push(startDate);
}
startDate = new Date((startDate.getTime() + this.millisecondPerDay));
}while(startDate <= endDate)
console.log("Calculated: "+this.disabledDates.length);
//console.log("Calculated: "+this.disabledDates);
}
IsSameDay(date1:Date, date2:Date)
{
if(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate())
{
return true;
}
else
{
return false;
}
}
}