转自别人,对你有用!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;

解决方案 »

  1.   

    decodetime(date,year,month,day)把时间分解成年月日
    剩下的就不用我说了把
      

  2.   

    时间相减如何得到秒数
    相减的结果 
    试一试以下代码: procedure TForm1.Button1Click(Sender: TObject); 
    var 
      dtr,dts,dte : TDateTime; 
      min : double; 
    begin 
      dts :=strtodatetime('2000-11-9 12:30'); 
      dte :=strtodatetime('2000-11-9 11:30'); 
      dtr :=dte-dts; 
      min :=dtr*24*60*60; 
      showmessage(floattostr(min)) 
    end; 
      

  3.   

    var
      year1,month1,day1,year2,month2,day2:word;
    begin
        DecodeDate(date1, Year1,Month1,Day1);
        DecodeDate(date2, Year2,Month2,Day2);
        Label1.Caption := inttostr(year1 - year2);
        Label2.Caption := inttostr(month1 - month2);
        Label3.Caption := inttostr(day1 - day2);
    end
      

  4.   

    var
       year ,month ,day :word ;
    begin
       DecodeDate(date2-date1 ,year ,month ,day) ;
       //year ,month ,day即为两个日期相差的年,月,日值
    end ;
      

  5.   

    uses DateUtils;var
      days :integer ;
    begin
       days := DaysBetween(date2, date1) ;
       //days即为相差天数
    end ;
      

  6.   

    uses DateUtils;var
      days :integer ;
    begin
       days := DaysBetween(date2, date1) ;
       //days即为相差天数
    end ;
      

  7.   

    var
      year ,month ,day :word ;
    begin
      DecodeDate(date2-date1 ,year ,month ,day) ;
      //year ,month ,day即为两个日期相差的年,月,日值
      //year*12+month就是相差月数啦
    end ;
      

  8.   

    这样可以得到year*12+month月份,那么天数怎么算呢?
      

  9.   

    //for Delphi6 DateUtils.pas
    { Range query functions }function YearsBetween(const ANow, AThen: TDateTime): Integer;
    function MonthsBetween(const ANow, AThen: TDateTime): Integer;
    function WeeksBetween(const ANow, AThen: TDateTime): Integer;
    function DaysBetween(const ANow, AThen: TDateTime): Integer;
    function HoursBetween(const ANow, AThen: TDateTime): Int64;
    function MinutesBetween(const ANow, AThen: TDateTime): Int64;
    function SecondsBetween(const ANow, AThen: TDateTime): Int64;
    function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;