Reputation: 728
I'm using bootstrap datetime-picker in angularjs
I disabled few days using the option dateDisabled of the date picker like this
$scope.dateOptions = {
dateFormat: 'yyyy-MM-dd',
maxDate: new Date(2020, 5, 22),
minDate: new Date(),
startingDay: 1,
onChangeDate: countDays,
dateDisabled: function (data) {
var date = new Date(data.date)
date.setHours(0, 0, 0, 0)
var date2 = new Date('2019-02-08')
date2.setHours(0, 0, 0, 0)
return (date == date2.toString());
}
};
Now need to calculate the number of days between the selected date and the current date based on the date picker i.e. disabled date should not be count in the days calcluation.
If i selected the date as 10 feb 2019 then the number of days to be count as 4 (using current date - 5 feb 2019 ).
but it is coming as 5
The function get calls when i select the date from date picker
function countDays(dateTime) {
var fourDaysLater = new Date();
fourDaysLater.setDate(dateTime.getDate() - 4);
}
How to count dates which are enabled in the date picker?
Upvotes: 1
Views: 1649
Reputation: 132
Answering your question
How to count dates which are enabled in the date picker?
You have disabled the dates using date picker but when you are counting the number of days between two dates you are using new date() which can't access your date picker's date.
You can do something like this -
function countDays(dateTime) {
// Current date
var currentDate = new Date().setHours(0, 0, 0, 0);
// Selected date
var selectedDate = new Date(dateTime).setHours(0, 0, 0, 0);
// Count working days between selected date and current date
while (currentDate < selectedDate) {
if (currentDate != new Date('2019-02-08').setHours(0, 0, 0, 0)) {
++workingDays;
}
currentDate.setDate(currentDate.getDate() + 1);
}
}
alert(workingDays); // Number of working days
Count the number of days between two days and exculde the
Upvotes: 1