网际凡人 <nbwsj@263.net>
function ToTime(DateTime:TDateTime;day,hour,minute,second:{加减
量}integer):TDateTime;
{给出一个具体时间,减去天数或小时、分钟、秒钟,即得到新时间}
var
hh,mm,ss,ms:word;
hx,mx,sx:integer; {时间中间值}
dt:TDate;
begin
dt :=DateTime;
DecodeTime(DateTime,hh,mm,ss,ms);
hx:=hh;
mx:=mm;
sx:=ss;
{秒}
sx:=sx+second;
if sx<0 then
begin
minute :=minute + (sx div 60)-1;
sx :=60 +(sx mod 60);
end
else if sx>=60 then
begin
minute :=minute+(sx div 60);
sx :=sx mod 60;
end;
{分}
mx :=mx+minute;
if mx<0 then
begin
hour :=hour+(mx div 60)-1;
mx :=60+(mx mod 60);
end
else if mx>=24 then
begin
hour :=hour+(mx div 60);
mx :=mx mod 60;
end;
{时}
hx :=hx+hour;
if hx<0 then
begin
day :=day+(hx div 24)-1;
hx :=24+(hx mod 24);
end
else if hx>=24 then
begin
day :=day+(hx div 24);
hx :=hx mod 24;
end;
{天}
dt :=dt+day;
DateTime :=StrToDateTime(DateToStr(dt)+' '+IntToStr(hx)+':'+IntToStr(mx)+
':'+IntToStr(sx)+':');
Result :=DateTime;
end;
这是尚在编制中的源码,求两个时间差:天数、小时、分钟等。
各位有兴趣,可自己编个函数!
procedure TForm1.Button3Click(Sender: TObject);
var
day,hour,minute,second,h,m,s:integer;
hh1,mm1,ss1,ms1,hh2,mm2,ss2,ms2:word;
dt1,dt2:TDateTime;
begin
{两个时间差}
dt1 :=StrToDateTime(Edit1.text);
dt2 :=StrToDatetime(Edit2.text);
DecodeTime(dt1,hh1,mm1,ss1,ms1);
DecodeTime(dt2,hh2,mm2,ss2,ms2);
day:=dt1-dt2; file://就日期不能相差,怎么办?
h:=hh1;
m:=mm1;
s:=ss1;
if s<ss2 then
begin
m:=m-1;
s:=s+60;
end;
second :=s-ss2;
if m<mm2 then
begin
h:=h-1;
m:=m+60;
end;
minute :=m-mm2;
if h<hh2 then
begin
day :=day-1;
h:=h+24;
end;
hour :=h-hh2;
edit3.Text :='相差'+IntToStr(day)+'天'+IntToStr(Hour)+'小时'+
IntToStr(minute)+'分'+IntToStr(Second)+'秒';
end;