Adding days to a Date

Posted on 2000-01-06
Medium Priority
Last Modified: 2010-04-06
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
Question by:Gunstar2
  • 2
  • 2

Author Comment

ID: 2331246
Edited text of question.

Accepted Solution

MattFlower earned 200 total points
ID: 2331265
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,


Expert Comment

ID: 2334820
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


Author Comment

ID: 2344325
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.

Expert Comment

ID: 2348112
I am happy that I could help.  :)


Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question