你还要不要计算天数,如果不用那就把时间取出来,做些处理转换成datatime格式,然后在
计算。下面的例子是计算时间的
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons;
type
Twatch = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
RealTimeEdit: TEdit;
StartTimeEdit: TEdit;
EndTimeEdit: TEdit;
EllapseTimeEdit: TEdit;
StartButton: TBitBtn;
StopButton: TBitBtn;
Timer1: TTimer;
Timer2: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure StartButtonClick(Sender: TObject);
procedure StopButtonClick(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
watch: Twatch;
StartTime : TdateTime;
EllapseTime:TdateTime;
implementation
{$R *.DFM}
procedure Twatch.FormCreate(Sender: TObject);
begin
StopButton.Enabled := False;
//使停止按钮无效
RealTimeEdit.Text := TimeToStr(time);
//将组件的Text属性设置为当前时间
StartTimeEdit.Text := '00:00:00';
//将以下三个组件的Text属性设置为00:00:00
EndTimeEdit.Text := '00:00:00';
EllapseTimeEdit.text := '00:00:00';
Timer2.enabled:=false;
end;
procedure Twatch.Timer1Timer(Sender: TObject);
begin
RealTimeEdit.Text := TimeToStr(Time);
// 用当前时间刷新Text
end;
procedure Twatch.StartButtonClick(Sender: TObject);
begin
Timer2.Enabled := True;
StopButton.Enabled := true;
StartTime := Time;
StartTimeEdit.Text := TimeToStr(StartTime);
end;
procedure Twatch.StopButtonClick(Sender: TObject);
begin
Timer2.Enabled := False;
StopButton.Enabled := False;
end;
procedure Twatch.Timer2Timer(Sender: TObject);
Var
EndTime : TdateTime;
i,Hour,Minute,Second,MilliSec:Word;
HS,MS,SS,MSS : string[2];
begin
Endtime := Time;
//取当前时间
EllapseTime := Time - StartTime;
// 计算用去时间
EndTimeEdit.Text := TimeToStr(EndTime);
// 设置结束时间文本
DecodeTime(EllapseTime,Hour,Minute,Second,MilliSec);
//进行时间换算
Str(Hour:2,HS);
str(Minute:2,MS);
str(Second:2,SS);
for i := 1 to 2 do
begin
if hs = ' ' then
hs := '0';
if ms = ' ' then
ms := '0';
if ss = ' ' then
ss := '0';
end;
Str((MilliSec div 100):1,MSS);
EllapseTimeEdit.Text := hs+':'+ms+':'+ss+'.'+mss;
//显示用去时间
end;
end.