Adding days to a Date

Using a db, I need to add a varable number of days to a date field to come up with an expire date. example - adding 30 days to 1/1/2000 would reslut in 1/31/2000. I am new to Delpi3, can you help? An example would be very helpful..
My db fields are
  VD1 = Varable date as an Integer
  CD1 = Current date as a date field
  ExP = Expire date as a date field
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gunstar2Author Commented:
Edited text of question.
It depends a little bit on how you are using the data.  If you are using a TQuery, I would recommend using SQL to create an additional field.

For example in MSSQL, you would use the following in your SQL:

  FirstDate = Table1.Date
  , ExpDate = DateAdd(Day, 1, Table1.Date)

If you are using a TTable, it is a bit different. You don't have control over the SQL, making it a bit more difficult.  Probably the best bet would be a calculated field.  A calculated field is one that appears to be part of the database, except that you are responsible for the calculation.

To create a calculated field, bring up the "fields" dialog of the TTable, and define a field.  Then implement the OnCalcFields Event.  The OnCalcFields Event will pass a dataset.  If you made a calculated field with a name like "ExpDate" in your event handler you would have code such as:

procedure TForm1.Table1CalcFields(Dataset: TDataSet);
  Dataset.FieldByName("ExpDate").AsDate := Dataset.FieldByName("Date1").AsDate + 30;

Remember that TDateTime is simply of type double.  In the above example I added 30 days.

Hope this helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hi, Mr Gunstar2, try this

// Determine the Beginning of the month
Function BegOfMonth( dDate : TDateTime ) : TDateTime ;
  Y,M,D : Word ;
  DecodeDate(dDate,Y,M,D) ;
  Result := ( dDate - D + 1 ) ;
End ;

// Determine the End of the month
Function EndOfMonth(dDate : TDateTime ) : TDateTime ;
  Y,M,D    : Word ;
  dumpDate : TDateTime ;
  dumpDate := dDate ;
  DecodeDate(dumpDate,Y,M,D) ;
  dumpDate := dDate + 35 - D ;
  DecodeDate(dumpDate,Y,M,D) ;
  Result := dumpDate - D ;
End ;

// Determine the week number of a given
// day in the year
Function WeekOfYear(dDate : TDateTime ) : Word ;
  Y,M,D       : Word ;
  FirstOfYear : TDateTime ;
   DecodeDate(dDate,Y,M,D) ;
   FirstOfYear := EncodeDate(Y,1,1) ;
   Result := Trunc(dDate - FirstOfYear) Div 7 + 1 ;
End ;

// Go to the specified month, forward
// or backward
// Function IncMonth(const Date:
   TDateTime; NumberOfMonths: Integer):
// please have a look at the Help
// [ Delphi 5 ]

procedure TForm1.ShowDateBtnClick(Sender: TObject);
  ExpDate : TDateTime ;
  x : integer ;
  ShortDateFormat := 'dd/mm/yyyy' ;
  DateSeparator      := '-' ;
  ExpDate                := Date ;
  Label1.Caption     := DateToStr
    (BegOfMonth(ExpDate)) ;
  Label2.Caption     := DateToStr
    (EndOfMonth(ExpDate)) ;
  Label3.Caption     := IntToStr
    (WeekOfYear(ExpDate))  ;
  Label4.Caption     := DateToStr
    (IncMonth(ExpDate,5)) ;
end ;

Happy New Year From apin

Gunstar2Author Commented:
Thanks Mat, out of the 2 answers I recived, yours seam to work (with a couple of changes) the best for my application.  I needed to be able to change the interval between the dates as well. By adding a Dataset field in place of the 30 day interval, and a couple of ( ), I was able to accomplish this. The code I ended up with looks something like this:

procedure TForm1.Table1CalcFields(Dataset: TDataSet);
     Dataset.FieldByName('expire').AsDateTime:= (Dataset.FieldByName('current').AsDateTime+(Dataset.FieldByName('interval').asInteger*30));

where Interval could = any number.
I am happy that I could help.  :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.