How to add days to date?

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

How to add days to date?

Postby hardina09 » Thu Sep 20, 2012 7:12 pm

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.
hardina09
 
Posts: 62
Joined: Tue Apr 24, 2012 9:46 pm

Re: How to add days to date?

Postby ROCLASI » Thu Sep 20, 2012 7:49 pm

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: How to add days to date?

Postby jgarfield » Thu Sep 20, 2012 8:17 pm

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);
}
Programmer.
adBlocks
http://www.adblocks.com
jgarfield
 
Posts: 223
Joined: Wed Sep 28, 2005 9:02 pm
Location: Boston, US

Re: How to add days to date?

Postby hardina09 » Thu Sep 20, 2012 9:19 pm

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.
hardina09
 
Posts: 62
Joined: Tue Apr 24, 2012 9:46 pm

Re: How to add days to date?

Postby ROCLASI » Fri Sep 21, 2012 12:14 am

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);
}
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: How to add days to date?

Postby omar » Fri Sep 21, 2012 9:55 am

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.
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: How to add days to date?

Postby hardina09 » Fri Sep 21, 2012 8:21 pm

Thank you Omar, GREAT JOB.
hardina09
 
Posts: 62
Joined: Tue Apr 24, 2012 9:46 pm


Return to How To

Who is online

Users browsing this forum: No registered users and 3 guests

cron