Можете ли вы попробовать приведенный ниже код.
var a = '28.09.2020';
var b = '03.10.2020';
var calculateNumberOfDaysPerMonthAndYear = function(a, b) {
function compareDatesOnBasedOfMonthAndYear(a, b) {
if (b[2] > a[2]) return 1;
if (b[2] < a[2]) return -1;
if (b[1] > a[1]) return 1;
if (b[1] < a[1]) return -1;
return 0;
}
function getNextMonth(a) {
var nextMonth = [ ...a ];
nextMonth[1] += 1;
if (nextMonth[1] === 13) {
nextMonth[1] = 1;
nextMonth[2] += 1;
}
return nextMonth;
}
var finalJson = {};
var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var splittedDateA = a.split('.').map(d => parseInt(d));
var splittedDateB = b.split('.').map(d => parseInt(d));
var key = splittedDateA[1] + '.' + splittedDateA[2];
finalJson[key] = daysInMonth[splittedDateA[1] - 1] + 1 - splittedDateA[0];
if (splittedDateA[2] % 4 === 0) finalJson[key] += 1;
var currentDate = getNextMonth(splittedDateA);
while(compareDatesOnBasedOfMonthAndYear(currentDate, splittedDateB)) {
key = currentDate[1] + '.' + currentDate[2];
finalJson[key] = daysInMonth[currentDate[1] - 1];
currentDate = getNextMonth(currentDate);
}
key = splittedDateB[1] + '.' + splittedDateB[2];
finalJson[key] = splittedDateB[0];
return finalJson;
};
console.log(calculateNumberOfDaysPerMonthAndYear(a, b));
Выходными данными будет объект JSON с ключом в виде комбинации месяца и года (например, 8.2020), а значением будет количество дней.
Надеюсь, это поможет