比如说我用当前系统时间减去一个时间,怎样可以得到它们之间的相差天数,小时数,分钟数。三个数据要分开得到。要怎样才能实现?

解决方案 »

  1.   

    查看一下sql自带的帮助,里面的date函数的说明中有你要的!
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, StdCtrls;type
      TForm1 = class(TForm)
        btn1: TButton;
        dtp1: TDateTimePicker;
        dtp2: TDateTimePicker;
        lbl1: TLabel;
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses DateUtils;{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    var
      aDateTime, aInterval: TDateTime;
    begin
      aDateTime := EncodeDateTime(YearOf(dtp1.Date), MonthOf(dtp1.Date), DayOf(dtp1.Date)
      , HourOf(dtp2.Time), MinuteOf(dtp2.Time), 0, 0);  aInterval := MinuteSpan(Now, aDateTime);
      
      lbl1.Caption := Format('相差天数%d,小时数%d,分钟数%d'
      , [
        Trunc(aInterval /60/24), Trunc(aInterval / 60), Trunc(aInterval) mod 60
      ]);
    end;end.
      

  3.   

    两个数相减,得到的浮点数的整数部分为天数,小数部分为时间。看帮助:The integral part of a Delphi TDateTime value is the number of days that have passed since 12/30/1899. The fractional part of the TDateTime value is fraction of a 24 hour day that has elapsed. Following are some examples of TDateTime values and their corresponding dates and times:0 12/30/1899 12:00 am
    2.75 1/1/1900 6:00 pm
    -1.25 12/29/1899 6:00 am
    35065 1/1/1996 12:00 am
    To find the fractional number of days between two dates, simply subtract the two values, unless one of the TDateTime values is negative. Similarly, to increment a date and time value by a certain fractional number of days, add the fractional number to the date and time value if the TDateTime value is positive. 
    When working with negative TDateTime values, computations must handle time portion separately. The fractional part reflects the fraction of a 24-hour day without regard to the sign of the TDateTime value. For example, 6:00 am on 12/29/1899 is ?.25, not ? + 0.25, which would be ?.75. There are no TDateTime values between ? and 0.程序:
    ========================procedure TForm1.Button2Click(Sender: TObject);
    var
    dat1,dat2:TDateTime;
    dif:Double;days,hours,mins,secs:Integer;
    begin
      dat1:=EncodeDate(2000,10,1);
      dat2:=Now;  dif:=dat2-dat1;  days:=Round(Int(dif));  dif:=dif-days;
      secs:=round(dif * 24*60*60);  hours:=secs div 3600;
      mins:=(secs - hours*3600) div 60;  showmessage(Format('%d ,%d, %d',[days,hours,mins]));
    end;
      

  4.   

    不明白你的意思,你是要得到类似2天3小时20分的效果还是要得到2天,51小时,51*60+20分钟这样的效果?keyz(keyz) 就可以啊。两种结果可以互相转换~
      

  5.   

    用TDateTime类型存储这两个时间;
    要计算两个时间的间隔,TDateTime相减即可;
    单独取出日月年,可用: yearof(),monthof(),dayof().
      

  6.   

    取出两个日期的相隔:好像有一个datebetween的函数吧,记不清了
      

  7.   

    delhi的日期单元引用一下
    unit DateUtils;
      

  8.   


    [Delphi] procedure DecodeTime(const DateTime: TDateTime; var Hour: Word; var Min: Word; var Sec: Word; var MSec: Word);
      

  9.   

    procedure DecodeDateTime(const AValue: TDateTime; out AYear: Word; out AMonth: Word; out ADay: Word; out AHour: Word; out AMinute: Word; out ASecond: Word; out AMilliSecond: Word);
      

  10.   

    用cast语句可以进行计算得出结果!T(天数)=cast((GetDate()-T1) as float)
    T(小时)=cast((GetDate()-T1) as float)*24
    T(分钟)=cast((GetDate()-T1) as float)*24*60
    T(秒钟)=cast((GetDate()-T1) as float)*24*60*60
      

  11.   

    tdatetime*86400就是秒了,剩下的自己算:)
      

  12.   

    var
     y,m,d:integer;
     hh,mm,ss:integer;
    begin
       DecodeDate(now,y,m,d);     --取出 年,月,日
       decodeTime(now,hh,mm,ss);  --取出 时,分,秒end;