2011-06-14 35 views
5

有没有人有一个简短而有效的代码hh:mm:ss timer(Timer1.Interval:= 1000)?我可以做一个,但我想要一些有效的东西。德尔福:简单的hh:mm:ss定时器

谢谢!

我的代码:

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; 
+1

根据您的问题中提供的信息不可能知道您想要做什么。你需要告诉我们你希望完成什么。你是否需要帮助将毫秒数转换为小时,分钟和秒,或者你想让数字时钟倒计时还是......? – 2011-06-14 01:01:44

+0

只是为了在标签中显示时间(hh:mm:ss)。 “将毫秒数转换为小时,分钟和秒”,或者如果可能的话不进行转换。 – maxfax 2011-06-14 01:11:59

+4

定义高效?最短的执行时间?最小的代码?最小的StackOverflow查找? – dthorpe 2011-06-14 01:50:57

回答

8

添加一个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. 

这应该表明采取任何间隔正确的时间。
5000每5秒显示一次所需的时间。

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

+0

+1对于使用FormatDateTime,但-1用于尝试在标记中放置双精度。 = 0 – NGLN 2011-06-14 01:53:21

+0

公平呼叫,我有时忘记TdateTime不是一个整数。 – 2011-06-14 01:57:14