I am working on a Onchange() event form.
By default, the Present Value as of date is today. If a user enters Remaining payments = 12 months then the
End of Lease = Present Value as of date + Remaining payment (i.e months).
For my onchange event i am trying to calculate Start of Lease automatically. Here, Start of Lease = End of lease - Remaining payment.
Here is my code so far. The issue i am having is that the Start of Lease date is getting reflected but it's returning a day less than End of Lease. However, the first part calculating the End of Lease itself is working fine.
function PopulateEndDate() {
debugger;
var d2;
var paymentDays;
var stDate;
var pDate;
var etDate;
var ddlFrequency = document.getElementById("ddFrequency");
var selectedFrequency = ddlFrequency.options[ddlFrequency.selectedIndex].value;
if (document.getElementById("startDate"))
stDate = document.getElementById("startDate").value;
if (document.getElementById("presentDate"))
pDate = document.getElementById("presentDate").value;
var today = new Date();
if (stDate)
d2 = new Date(stDate);
else
d2 = new Date(pDate);
if (document.getElementById("paymentRemaining")) {
if (selectedFrequency == "D") {
paymentDays = document.getElementById("paymentRemaining").value;
}
else if (selectedFrequency == "Q") {
paymentDays = document.getElementById("paymentRemaining").value * 3;
}
}
if (paymentDays && paymentDays != "")
d2.setMonth(d2.getMonth() + parseInt(paymentDays));
if (document.getElementById("endDate"))
document.getElementById("endDate").value = getCurrentDay(d2);
var endDate = document.getElementById("endDate").value;
if (endDate)
etDate = new Date(endDate);
if (stDate == null && paymentDays && paymentDays != "")
{
etDate.setMonth(etDate.getMonth() - parseInt(paymentDays));
}
if (document.getElementById("startDate"))
document.getElementById("startDate").value = getCurrentDay(etDate);
}
I am not sure what i am doing wrong here. Any help will be greatly appreciated. Thank you.