做个假设:
时间1:1991年2月15日
时间2: 2013年3月16日希望得到两个时间的差距值,并以年月日返回。

解决方案 »

  1.   

    select 日差=datediff(day,日期1,日期2) 这个是求两个日期之间的差的,返回是int,你要求的这个距离,就不知道了,关注下。
      

  2.   


    var
      s, e: TTimeStamp;
      tt: Double;
    begin
      s := DateTimeToTimeStamp(timeStart);
      e := DateTimeToTimeStamp(timeEnd);
      tt := (e.Time - s.Time) / 1000; // 得到两个时间相差了的秒数,自己举一反三
      

  3.   

    var
     date1,date2:TDate;
    begin
    date1:=DateTimePicker1.Date;
    date2:=DateTimePicker2.Date;ShowMessage('共差'+IntToStr(DaysBetween(date1,date2))+'天');智能得到的是天数,但不是想象中的xxxx年xx月xx日的效果!
      

  4.   

    这是你要的效果吗?
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DateUtils, ComCtrls, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        DateTimePicker1: TDateTimePicker;
        DateTimePicker2: TDateTimePicker;
        DateTimePicker3: TDateTimePicker;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function TimeBetween(A, B: TDateTime): string;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      Memo1.Lines.Add(TimeBetween(DateTimePicker1.Date, DateTimePicker2.Date));
    end;function TForm1.TimeBetween(A, B: TDateTime): string;
    var
      Y, M, Day: Integer;
      C, D: TDateTime;
      sA, sB: string;
    begin
      if Trunc(A) > Trunc(B) then
      begin
        Result := 'Error';
        exit;
      end;  if Trunc(A) = Trunc(B) then
      begin
        Result := '0Y0M0D';
        exit;
      end;  Y := YearsBetween(B, A);  M := MonthsBetween(B, A);
      M := M - Y * MonthsPerYear;
      Day := DaysBetween(B, A) - Trunc(Y * ApproxDaysPerYear) - Trunc(M * ApproxDaysPerMonth);
      Result := IntToStr(Y) +'Y'+ IntToStr(M) +'M'+ IntToStr(Day) +'D';
    end;end.
      

  5.   

    TDate 和 TDateTime 都是Double类型,可以直接相减的,或者可以去 DateUtils 单元,这个单元是专门针对日期的操作。
      

  6.   

    YearsBetween,MonthsBetween,DaysBetween函数,记得引用DateUtils单元。不过Delphi5不支持这些函数,Delphi7有,delphi6不清楚