德尔福:简单的hh:mm:ss Timer

max*_*fax 6 delphi timer

有没有人为hh:mm:ss计时器(Timer1.Interval:= 1000)提供简短有效的代码?我可以制作一个,但我想要一些有效的东西.

谢谢!

我的代码:

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;
Run Code Online (Sandbox Code Playgroud)

Chr*_*ase 9

添加一个Var以跟踪计时器何时启动.

  TForm1 = class(TForm)
  private
    timerStart: TDateTime;
  public
    proceure StartTimer;
  end;
Run Code Online (Sandbox Code Playgroud)

程序启动计时器

proceure TForm1.StartTimer;
beign
 timerStart := now();
 timer1.interval = 1000;
 timer1.enabled := true;
end;
Run Code Online (Sandbox Code Playgroud)

在OnTimer活动中

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

这应该显示任何间隔所需的正确时间.
5000每秒显示一次,所用的时间.

注意:如果不进行测试,运行计时器超过24小时可能无法显示正确的时差.为此,我认为日期时间格式字符串应该类似于dd hh:nn:ss显示过去的日子