两个TDateTime类型变量如何求出其相差的秒数?

解决方案 »

  1.   

    两者直接相减即得到秒数,如 DeltaD := dt1-dt2;
      

  2.   

    uses DateUtils;function SecondsBetween(const ANow, AThen: TDateTime): Int64;
      

  3.   

    计算时间差的函数
    function ToTime(DateTime:TDateTime;day,hour,minute,second:{加减量}integer):TDateTime;{给出一个具体时间,减去天数或小时、分钟、秒钟,即得到新时间}varhh,mm,ss,ms:word;hx,mx,sx:integer; {时间中间值}dt:TDate;begindt :=DateTime;DecodeTime(DateTime,hh,mm,ss,ms);hx:=hh;mx:=mm;sx:=ss;{秒}sx:=sx+second;if sx<0 thenbeginminute :=minute + (sx div 60)-1;sx :=60 +(sx mod 60);endelse if sx>=60 thenbeginminute :=minute+(sx div 60);sx :=sx mod 60;end;{分}mx :=mx+minute;if mx<0 thenbeginhour :=hour+(mx div 60)-1;mx :=60+(mx mod 60);endelse if mx>=24 thenbeginhour :=hour+(mx div 60);mx :=mx mod 60;end;{时}hx :=hx+hour;if hx<0 thenbeginday :=day+(hx div 24)-1;hx :=24+(hx mod 24);endelse if hx>=24 thenbeginday :=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);varday,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 thenbeginm:=m-1;s:=s+60;end;second :=s-ss2;if m<mm2 thenbeginh:=h-1;m:=m+60;end;minute :=m-mm2;if h<hh2 thenbeginday :=day-1;h:=h+24;end;hour :=h-hh2;edit3.Text :='相差'+IntToStr(day)+'天'+IntToStr(Hour)+'小时'+IntToStr(minute)+'分'+IntToStr(Second)+'秒';end; 
     
      

  4.   

    hoho 来DELPHI版几天见的最多的是 爱的眼睛 && 楼上的了 ^_^