【问题标题】:Delphi: Simple hh:mm:ss TimerDelphi:简单的 hh:mm:ss 计时器
【发布时间】:2013-12-05 18:51:08
【问题描述】:

有没有人有一个简短有效的 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;

【问题讨论】:

  • 仅根据您问题中提供的信息,不可能知道您想做什么。您需要告诉我们您到底希望完成什么。您是否需要帮助将毫秒数转换为小时、分钟和秒,或者您需要动画数字时钟倒计时,还是...?
  • 只是为了在标签中显示时间 (hh:mm:ss)。 “将毫秒数转换为小时、分钟和秒”,或者在可能的情况下不进行转换。
  • 定义高效?最短的执行时间?最少的代码?最少的 StackOverflow 查找?

标签: delphi timer


【解决方案1】:

添加一个 Var 来跟踪计时器的启动时间。

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

启动定时器的过程

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

在 OnTimer 事件中

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

这应该显示任何间隔所用的正确时间。
每 5 秒显示 5000 所用的时间。

注意:未经测试,运行计时器超过 24 小时可能无法显示正确的时差。对于那个我认为日期时间格式字符串应该类似于dd hh:nn:ss来显示过去的天数

【讨论】:

  • +1 用于使用 FormatDateTime,但 -1 用于尝试在 Tag 中放入双精度。 = 0
  • 公平调用,我有时会忘记 TdateTime 不是 int。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
相关资源
最近更新 更多