Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: Simple hh:mm:ss Timer

Tags:

timer

delphi

Does anybody have a short and effective code for hh:mm:ss timer (Timer1.Interval:=1000)? I can make one but I want something efficient.

Thanks!

My Code:

Var MyTime:TTime;

MyTime:=EncodeTime(0,0,0,0);

procedure TForm1.Timer1Timer(Sender: TObject);
begin
MyTime:=incsecond(Mytime,1);
form1.Label1.Caption:='Time: '+TimeToStr(MyTime);
end;
like image 770
maxfax Avatar asked Jun 14 '11 00:06

maxfax


1 Answers

Add A Var to keep track of when the timer started.

  TForm1 = class(TForm)
  private
    timerStart: TDateTime;
  public
    proceure StartTimer;
  end;

The procedure To start the Timer

proceure TForm1.StartTimer;
beign
 timerStart := now();
 timer1.interval = 1000;
 timer1.enabled := true;
end;

In the OnTimer Event

Label1.caption := formatdatetime('hh:nn:ss', timerStart - now()); //nn is for minutes.

This should show the correct time taken for any interval.
i.e. 5000 to show every 5 seconds, the time taken.

Note: With out testing, running the timer for longer than 24 hours might not show the correct time difference. For that I think that the datetime format string should be something like dd hh:nn:ss to show the days passed

like image 90
Christopher Chase Avatar answered Nov 07 '22 20:11

Christopher Chase