Page 1 of 1

How to add days to date?

PostPosted: Thu Sep 20, 2012 7:12 pm
by hardina09
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

Code: Select all

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.

Re: How to add days to date?

PostPosted: Thu Sep 20, 2012 7:49 pm
by ROCLASI
Hi,

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:
Code: Select all
for (var i = 0 ; i<value ; i++ ) {
    startdate.setDate(startdate.getDate() + 1)
}


Hope this helps.

Re: How to add days to date?

PostPosted: Thu Sep 20, 2012 8:17 pm
by jgarfield
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)

Code: Select all
for (var i = 0 ; i<value ; i++ ) {
    startdate.setDate(startdate.getDate() + 1)
    record.mydate = new Date(startdate);
}

Re: How to add days to date?

PostPosted: Thu Sep 20, 2012 9:19 pm
by hardina09
Code: Select all
for (var i = 0 ; i<value ; i++ ) {
    startdate.setDate(startdate.getDate() + 1)
    record.mydate = new Date(startdate);
}


Still date does not change, 1 is not added to date.. It is storing same date value for entire loop does not increment date value.

Re: How to add days to date?

PostPosted: Fri Sep 21, 2012 12:14 am
by ROCLASI
What does the following output for you when you run this in Developer ?
(adjust the syntax to match your variable-/column names)

Code: Select all
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);
}

Re: How to add days to date?

PostPosted: Fri Sep 21, 2012 9:55 am
by omar
Hi Hardina,

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:

Code: Select all
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:

Code: Select all
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.

Re: How to add days to date?

PostPosted: Fri Sep 21, 2012 8:21 pm
by hardina09
Thank you Omar, GREAT JOB.