Monday, January 10, 2011

calculating difference of time in Delphi

: How can I calculate a difference of time?
: I have a program that begin at 12:00:10,200 and
: finish at 12:10:00,560 for example.
: I need to calculte the difference between two.

Declare two Delphi TDateTime variables, e.g. T1 for the starting time and T2 for the ending time.
To measure and display the time span that a process takes:

T1 := Now;
Label1.Caption := FormatDateTime('"Start: " hh:nn:ss.zzz', T1);
// Process that you want to time:...
// ...
T2 := Now;
Label2.Caption := FormatDateTime('"Stop: " hh:nn:ss.zzz', T2);
Label3.Caption := FormatDateTime('"Diff: " hh:nn:ss.zzz', T2 - T1);
Try another code below
Get the time difference between one event and another one :
Initialize start and elapsed time variables that are visible to both events:

  StartTime: TDateTime;
  ElapsedTime: TDateTime;

procedure YourForm.Event1(Sender: TObject);
  StartTime := Now;

procedure YourForm.Event2(Sender: TObject);
  ElapsedTime := Now - StartTime;
  Label1.Caption := FormatDateTime('', ElapsedTIme);

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum