Link to home
Start Free TrialLog in
Avatar of Gunstar2
Gunstar2Flag for United States of America

asked on

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
Avatar of Gunstar2
Gunstar2
Flag of United States of America image

ASKER

Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of MattFlower
MattFlower

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pc_melsa
pc_melsa

Hi, Mr Gunstar2, try this

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

// Determine the End of the month
Function EndOfMonth(dDate : TDateTime ) : TDateTime ;
Var
  Y,M,D    : Word ;
  dumpDate : TDateTime ;
Begin
  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 ;
Var
  Y,M,D       : Word ;
  FirstOfYear : TDateTime ;
Begin
   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):
   TDateTime;
// please have a look at the Help
// [ Delphi 5 ]

procedure TForm1.ShowDateBtnClick(Sender: TObject);
Var
  ExpDate : TDateTime ;
  x : integer ;
Begin
  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


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);
begin
     Dataset.FieldByName('expire').AsDateTime:= (Dataset.FieldByName('current').AsDateTime+(Dataset.FieldByName('interval').asInteger*30));
end;

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

-Matt