0

I am working on a Onchange() event form.

Form example

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.

0

1 Answer 1

1
if (endDate){
var etDate = new Date(endDate);
var eDay = etDate.getUTCDate()+1;
var eMonth = etDate.getUTCMonth()+1;
var eYear = etDate.getUTCFullYear()-1;
var sDate = eYear + "-" + eMonth + "-" + eDay;

    if (stDate == null)
    {
        sDate.setMonth(sDate.getDay() - parseInt(paymentDays));
    }

   if (document.getElementById("startDate"))
        document.getElementById("startDate").value = getCurrentDay(sDate);

This helped solve my problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.