Gunstar2
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
My db fields are
VD1 = Varable date as an Integer
CD1 = Current date as a date field
ExP = Expire date as a date field
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(Se nder: 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
// 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(Se
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
ASKER
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(Da taset: TDataSet);
begin
Dataset.FieldByName('expir e').AsDate Time:= (Dataset.FieldByName('curr ent').AsDa teTime+(Da taset.Fiel dByName('i nterval'). asInteger* 30));
end;
where Interval could = any number.
procedure TForm1.Table1CalcFields(Da
begin
Dataset.FieldByName('expir
end;
where Interval could = any number.
I am happy that I could help. :)
-Matt
-Matt
ASKER