I am trying to implement the formula from Excel using the JSON object provided below. I have done the computations, but there seems to be an error as the result is not exact. If anyone has any ideas about it, I would appreciate your input. Thank you.
I just want to verify if I made a mistake in the conversion. The correct result should be based on the JSON data below:
https://i.sstatic.net/p1Nwm.png
https://i.sstatic.net/0XLJ2.png
#FORMULA
=IF(AND(H4="Current",I4<$B$6,J4>$B$6),DAYS360($B$6,J4)/360*12,IF(AND(H4="Current",I4>$B$6,J4>$B$6),DAYS360(I4,J4)/360*12,0))
#sample data input (the scheduleData object and effectiveDate input)
effectiveDate = new Date('11/1/2021')
scheduleData= [
{
"description": "Current Term",
"startDate": "6/1/2021",
"endDate": "04/30/2025",
"annualRent": 359659.19999999995
},
{
"noticeDate": "04/30/2028",
"description": "Current Term - Rent Adjustment",
"startDate": "05/01/2025",
"endDate": "04/30/2029",
"annualRent": 377642.16000000003
}
]
Compute(scheduleData: any):any{
let startDate = typeof scheduleData.startDate === 'string' ? new Date(scheduleData.startDate):scheduleData.startDate;
const endDate = typeof scheduleData.endDate === 'string' ? new Date(scheduleData.endDate):scheduleData.endDate;
if(!startDate || startDate.toString() === 'Invalid Date'){
}
let monthlyRent = scheduleData.annualRent / 12;
let monthsInPeriod = (this.Days360(startDate, endDate) / 360) * 12
let rentInPeriod = monthsInPeriod * monthlyRent;
return {
description: scheduleData.description,
monthlyRent: monthlyRent,
monthsInPeriod: monthsInPeriod,
rentInPeriod: rentInPeriod
}
}
Days360(startDate: Date, endDate: Date, decimalPlace: number = 2){
if(!startDate || !endDate){
return undefined;
}
let startDay = startDate.getUTCDate();
let endDay = endDate.getUTCDate();
if(isLastDayOfMonth(startDate)){
startDay = 30;
}
if(isLastDayOfMonth(startDate) && isLastDayOfMonth(endDate)){
endDay = 30;
}
const computedDays = (((endDate.getUTCFullYear() - startDate.getUTCFullYear()) * 360) + ((endDate.getUTCMonth() - startDate.getUTCMonth()) * 30) + (endDay - startDay));
return parseFloat(parseFloat(computedDays.toString()).toFixed(decimalPlace));
}