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;
同意 死树,delphi都做好了的
var Date1,Date2:TDateTime; i:real; begin ... Date1:=Now; Date2:=StrToDate('2001-11-10'); i:=Int(Date1-Date2);//i就是2个时间差的天数 showmessage(floatToStr(i)); end;
DaysBetween(date1, date2)要先Use DateUtils;
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;
Date1,Date2:TDateTime;
i:real;
begin
...
Date1:=Now;
Date2:=StrToDate('2001-11-10');
i:=Int(Date1-Date2);//i就是2个时间差的天数
showmessage(floatToStr(i));
end;