[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Date Calculation

Posted on 1999-11-29
4
Medium Priority
?
312 Views
Last Modified: 2010-04-04
I need to calculate how many days have elapsed between two given dates. i.e...

29/11/1999 - 21/11/1999 = 8

Cheers, Chris.
0
Comment
Question by:she3i3i
  • 2
4 Comments
 
LVL 1

Expert Comment

by:Heapster
ID: 2242371
Tdatetimes are stored as floats with the integer portion representing the day and the fractional part the time.  So you simple subtract one date from the other and add 1.  

Here is code for a unit that does this.  Drop 2 datetimepickers and a button. The trunc call is to make sure there is no time component included in the date.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
 showmessage(inttostr(trunc(datetimepicker1.date - datetimepicker2.date + 1)));
end;

end.
0
 
LVL 1

Author Comment

by:she3i3i
ID: 2242843
Heapster,

   this seems to work but it returns negative values

   i.e. -17 days

   any ideas why?

Cheers, Chris.
0
 
LVL 3

Accepted Solution

by:
sburck earned 40 total points
ID: 2242879
Try


Function DiffInDays(T1,T2 : TDateTime) : Integer;
begin
       if (T1 < T2) then
           result := trunc(T2)-trunc(T1);
       else
           result := trunc(T1)-trunc(T2);
end;

This will always return a positive result in days.

I disagree with the +1 that Heapster added - the result here should be zero when the same day is passed for both parameters, we're not counting one of the days, but the difference in days.
0
 
LVL 1

Expert Comment

by:Heapster
ID: 2244973
Yes this function (from sburck)is fine.  I was just intending to illustrate that you could work with tdatetimes as floating point numbers, so you could write your own function, rather writing a function myself to do the difference.  Very lazy!  
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses
Course of the Month7 days, 16 hours left to enroll

607 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