I am using two Calendar elements in my application one for start date and other for end date. 2 form variables that holds both values. I got difference of two dates in days. Say, for example
Start date is 09-20-2012 //(mm-dd-yyy)
End date is 09-24-2012
then no. of days would be 5 which is stored in some variable say “value”//Added some logic to get number of days
What I am doing is i am iterating until value is 5 and adding "value " to start date at each iteration. which is below
for(var i-0;i<value;i++)
{
startdate.setDate(startdate.getDate()+i) ////stores original value in "startdate"
//some Code & store value in new record in table
//Result should be 09-20-2012, 09-21-2012 09-22-2012 // So on....
}
Somehow I am not able to get to add value 1 to startdate. Where I went wrong. Any suggestions.
You should add just 1 to the startDate each time instead of i or else you add 1, 3, 6, etc. days to the original date.
Also you have a typo in the var i declaration ( - instead of = ).
So you code should look like this:
for (var i = 0 ; i<value ; i++ ) {
startdate.setDate(startdate.getDate() + 1)
}
You will also want to make sure that whenever you are storing the date in your record that you make a new date object for it, or everytime you go through your loop, you will change all records that have been set with startdate (because they will all be an instance of the same object, instead of new objects)
for (var i = 0 ; i<value ; i++ ) {
startdate.setDate(startdate.getDate() + 1)
record.mydate = new Date(startdate);
}
What does the following output for you when you run this in Developer ?
(adjust the syntax to match your variable-/column names)
for (var i = 0 ; i<value ; i++ ) {
startdate.setDate(startdate.getDate() + 1);
application.output(startdate);
// shouldn't this add/select a record first ?
record.mydate = new Date(startdate);
application.output(startdate + " : " + record.mydate);
}
Are you, by any chance, passing startdate to a function as a parameter? I’ve seen similar situations where the date is changed inside a function and stays the same outside of it. Perhaps these functions can help as they avoid the previous problem:
GODATE() adds a specified number of days to a given date:
function GODATE(dDate, nDays){
var dTemp = new Date(dDate.valueOf());
return new Date(dTemp.setDate(dTemp.getDate() + nDays));
}
DAYSBETWEEN calculates the number of days between two dates:
function DAYSBETWEEN(dDate1, dDate2, lIncludeTime){
var nDays=0;
if(!lIncludeTime){
dDate1=new Date(dDate1.getFullYear(),dDate1.getMonth(),dDate1.getDate());
dDate2=new Date(dDate2.getFullYear(),dDate2.getMonth(),dDate2.getDate());
nDays=Math.round((dDate1.valueOf()-dDate2.valueOf())/(60*60*24*1000));
}else{
nDays=(dDate1.valueOf()-dDate2.valueOf())/(60*60*24*1000);
}
return nDays;
}
These are part of the VFP2Servoy Toolkit and were provided by Juan Antonio Santana Medina.