This topic is locked
[SOLVED]

 Javascript onLoad to calculate expiry date

2/24/2018 12:22:24 AM
PHPRunner General questions
woodey2002 author

Hi Guys,
I am trying to create a javascript onLoad event on an add page to calculate on the fly when a students course has expired.
So far,
I have DATE field called "training_Date"
I also have an INT field called "duration_In_Days" (this field contains the 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 by taking the "training_Date" value i.e. 01/01/2001 and adding the value form the "duration_In_Days" i.e. 250 which would give me a date of expiry
I plan to display the results of this calculation in a "expiry_Date" field.
Using various examples I have cobbled together the following event with no Luck!
I would love to hear your thoughts, Is this possible?


var ctrlDuration = Runner.getControl(pageid, 'duration_In_Days');

ctrlDuration.on('change', function(e){
var ctrlExpiry = Runner.getControl(pageid, 'expiry_Date');
var ctrlTd = Runner.getControl(pageid, 'training_Date');

var d1 = new Date(this.getValue());

var d2 = new Date(ctrlTd .getValue());

var millisecondsPerDay = 1000 * 60 * 60 * 24;

var millisBetween = d1.getTime() - d2.getTime();

var days = millisBetween / millisecondsPerDay;
function func() {
ctrlExpiry .setValue(Number(ctrlTd.getValue()) + Number(ctrlDuration.getValue()));
};
ctrlDuration.on('keyup', func);
ctrlExpiry.on('keyup', func);
});


Best wishes,

J

admin 2/24/2018

Sure, you can do this. Right now your calculations are sort of meaningless, you try to calculate something but not doing anything with results of this calculation.
If regards to adding a certain number of days to existing date check this:

https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date

woodey2002 author 3/1/2018

Hi Guys
Just in case anyone is interested in how to to do this here is a synopsis of the problem I was trying to fix and the CORRECT CODE (thanks Sergey) used to solve it!
Problem
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