This topic is locked

Date plus adding selected days

5/28/2016 5:19:27 PM
PHPRunner General questions
M
Mark Kramer author

I'm trying to add days to today's date using a drop down list and populate the due date field. I have it working with the code if I put a number in manually but not when I use the drop down list.. Please help <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=23880&image=1&table=forumtopics' class='bbc_emoticon' alt=':)' /> Thank you
var d1 = Runner.getControl (pageid, 'Date Of Last Service');

var d2 = Runner.getControl (pageid, 'Maint Cycle in Days');

var d3 = Runner.getControl(pageid,'Date Started');

var d4 = Runner.getControl (pageid, 'Due');

var d5 = Runner.getControl(pageid,'Date Completed');
function addDays(theDate, days) {

return new Date(theDate.getTime() + days2460601000);

}
// below works when I enter the number 5 but when I try to replace it with the var da2, it tells me da2 undefined. I have moved the code up and down but still can't get it to work...

var newDate = addDays(new Date(), 5);
d4.setValue(newDate)
function func() {
if (d2.getValue()=="30 Day"){ (da2=30);

}

if (d2.getValue()=="60 Day"){ (da2=60);

}

if (d2.getValue()=="90 Day"){ (da2=90);

}

if (d2.getValue()=="120 Day"){ (da2=120);

}

if (d2.getValue()=="180 Day"){ (da2=180);

}

if (d2.getValue()=="365 Day"){ (da2=365);
};

d2.on('change', func);

}

Sergey Kornilov admin 5/31/2016

Variable da2 is in fact not defined anywhere. If you define it somewhere it will fix the error but I don't see how do you use it except assigning a value to it.

M
Mark Kramer author 5/31/2016



Variable da2 is in fact not defined anywhere. If you define it somewhere it will fix the error but I don't see how do you use it except assigning a value to it.


Thanks for your reply. BTW PHPrunner is an A+++ program! <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=79482&image=1&table=forumreplies' class='bbc_emoticon' alt=':D' />
The var da2 not being assigned was a newbie mistake and not all of the code was uploaded. It has been since revised and here is what I'm trying to do. I want to add Maintenance cycle days from drop down list "var d2"

I then set the value from it based on the selection to "var da2".
I'm trying to add da2 to today's date and display it in field "d4"
So here is the code....
var d1 = Runner.getControl (pageid, 'Date Of Last Service');

var d2 = Runner.getControl (pageid, 'Maint Cycle in Days');

var d3 = Runner.getControl(pageid,'Date Started');

var d4 = Runner.getControl (pageid, 'Due');

var d5 = Runner.getControl(pageid,'Date Completed');

var da2;
function func() {
if (d2.getValue()=="30 Day"){ (da2=30);

}

else

{

(da2=0);

}

if (d2.getValue()=="60 Day"){ (da2=60);

}

else

{

(da2=0);

}

if (d2.getValue()=="90 Day"){ (da2=90);

}

else

{

(da2=0);

}

if (d2.getValue()=="120 Day"){ (da2=120);

}

else

{

(da2=0);

}

if (d2.getValue()=="180 Day"){ (da2=180);

}

else

{

(da2=0);

}

if (d2.getValue()=="365 Day"){ (da2=365);

}

else

{

(da2=0);
};
}
function addDays(theDate, days) {

return new Date(theDate.getTime() + days2460601000)+(da2);

}

var newDate = addDays(new Date(), 0);
d4.setValue(newDate)
d2.on('Change', func);

alert (da2);

Sergey Kornilov admin 6/1/2016

I'm sorry to say that is code is completely off. Some parts of it do make sense but it is not tied together. The whole if ... else ... part in func() function is a mess. No matter what you select in dropdown box da2 variable will always be 0 because of all those else sections.
addDays() functions is trying to deal with both days and da2 variables. I would guess it only need to work with one of those variables.
Also the part where you calculate the new date and assign it to d4 control needs to be somewhere inside func() function.

M
Mark Kramer author 6/1/2016

OK I got it to work by parsing the variable . I was getting string data instead of a number. Again a newbie error <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=79492&image=1&table=forumreplies' class='bbc_emoticon' alt=':D' /> The only issue I'm having now is the calculated date is displayed but is not being saved when I add or edit the record..... Any thoughts?
Here is the working code for anyone who can use it.
var d1 = Runner.getControl (pageid, 'Date Of Last Service');

var d2 = Runner.getControl (pageid, 'Maint Cycle in Days');

var d3 = Runner.getControl(pageid,'Date Started');

var d4 = Runner.getControl (pageid, 'Due');

var d5 = Runner.getControl(pageid,'Date Completed');

var da2 = parseInt ();
function func() {
if (d2.getValue()=="30 Day"){ (da2=30);

}

if (d2.getValue()=="60 Day"){ (da2=60);

}

if (d2.getValue()=="90 Day"){ (da2=90);

}

if (d2.getValue()=="120 Day"){ (da2=120);

}

if (d2.getValue()=="180 Day"){ (da2=180);

}

if (d2.getValue()=="365 Day"){ (da2=365);
};
function addDays(theDate, days) {

return new Date(theDate.getTime() + days2460601000);

}

var newDate = addDays(new Date(), da2); //da2 sets number of days
d4.setValue(newDate);

}
d2.on('click', func);