This topic is locked

Javascript onLoad to calculate expiry date

3/1/2018 7:17:00 PM
PHPRunner Tips and Tricks
woodey2002 author

Scenario
I have a detail table called "staff_mandatory_study_days" who's master is the staff table
In this detail table... I'm working on a Javascript onload event...
I have DATE field called "staff_mandatory_Study_Day_Date'
I also have an INT field called "staff_mandatory_Study_Day_Duration" (this field contains the set amount of days until the course will expire i.e. 190).
My aim is to calculate the future date of which a students course will expire.
So by taking the "staff_mandatory_Study_Day_Date" value i.e. 01/01/2018 and adding the value form the ""staff_mandatory_Study_Day_Duration"" i.e. 62 which would give me a date of expiry being the 03/03/2018.
I plan to display the results of this calculation as a date i.e. 03/03/2018 in a "staff_mandatory_Study_Day_Expires" field.
The date of expiry should now been added to the "staff_mandatory_Study_Day_Expires" date field,
Using this value which is in a date format I next need to know how many DAYS are remaining from today.
So by comparing the date in "staff_mandatory_Study_Day_Expires" against today's date I would like the total number of days remaining from today with the answer being populated in the "staff_mandatory_Study_Day_Remaining" field. for example 7.
WORKING CODE



var ctrDuration = Runner.getControl(pageid, 'staff_mandatory_Study_Day_Duration');

var ctrDayDate = Runner.getControl(pageid, 'staff_mandatory_Study_Day_Date');

var ctrDayExp = Runner.getControl(pageid, 'staff_mandatory_Study_Day_Expires');

var ctrDayRem = Runner.getControl(pageid, 'staff_mandatory_Study_Day_Remaining');
ctrDayDate.on('change',function(){

setDates();

});

ctrDuration.on('change',function(){

setDates();

});
function setDates(){

if(ctrDayDate.getValue() && ctrDuration.getValue()){

var ddate = new Date(ctrDayDate.getValue());

ddate.setDate(ddate.getDate() + parseInt(ctrDuration.getValue()));

var out = (ddate.getMonth() + 1)+"/"+ddate.getDate()+"/"+ddate.getFullYear();

ctrDayExp.setValue(out);

var currDate = new Date();

var cDate = currDate.getTime();

var eDate = ddate.getTime();

ctrDayRem.setValue(parseInt((eDate - cDate)/1000/60/60/24));

}

}


Hope this helps!
J