type TTimeCompareType=set of (tcSecond,tcMinute,tcHour,tcDay,tcWeek,tcMonth,tcYear); function TPublicUnit.TimeBetween(Time1, Time2: TDateTime;CompareType:TTimeCompareType): Longint; var HourResult, MinuteResult, SecondResult ,MiscSencondResult: word; TimeDiff: TDateTime; Sign:boolean; retval:LongInt; begin if Time1 < Time2 then begin TimeDiff := Time2 - Time1; Sign:=false; end else begin TimeDiff := Time1 - Time2; Sign:=true; end; DecodeTime(TimeDiff, HourResult, MinuteResult, SecondResult,MiscSencondResult); if tcSecond in CompareType then begin retval := (HourResult * 3600) + (MinuteResult * 60)+ SecondResult; end else if tcMinute in CompareType then begin retval := (HourResult * 60) + MinuteResult; end else if tcHour in CompareType then begin retval := HourResult; end else begin retval:=-1; Sign:=true; end; if (not Sign) then TimeBetween:=-(retval) else TimeBetween:=retval; end;
function SecondsBetween(const ANow, AThen: TDateTime): Int64;DescriptionCall SecondsBetween to obtain the difference, in seconds, between two TDateTime values. SecondsBetween counts only entire seconds that have elapsed. Thus, SecondsBetween reports the difference between 9:00:00 AM and 9:00:00:999 AM as 0 because the difference is one millisecond short of an entire second.
怎么会呢?我实验过啊。看看DELPHI自己怎么说的type TDateTime = type Double;To find the fractional number of days between two dates, simply subtract the two values, 看,就是这样啊,你怎么个不对法?你的时间是怎么取到的?是不是后面有微秒的?那样就有小数了。
declare @b datetimeset @a='2002-04-26 23:00'
set @b='2002-05-01 8:05'select datediff(second,@a,@b)
TTimeCompareType=set of (tcSecond,tcMinute,tcHour,tcDay,tcWeek,tcMonth,tcYear);
function TPublicUnit.TimeBetween(Time1, Time2: TDateTime;CompareType:TTimeCompareType): Longint;
var
HourResult, MinuteResult, SecondResult ,MiscSencondResult: word;
TimeDiff: TDateTime;
Sign:boolean;
retval:LongInt;
begin
if Time1 < Time2 then begin
TimeDiff := Time2 - Time1;
Sign:=false;
end
else begin
TimeDiff := Time1 - Time2;
Sign:=true;
end; DecodeTime(TimeDiff, HourResult, MinuteResult, SecondResult,MiscSencondResult); if tcSecond in CompareType then begin
retval := (HourResult * 3600) + (MinuteResult * 60)+ SecondResult;
end
else if tcMinute in CompareType then begin
retval := (HourResult * 60) + MinuteResult;
end
else if tcHour in CompareType then begin
retval := HourResult;
end
else begin
retval:=-1;
Sign:=true;
end; if (not Sign) then
TimeBetween:=-(retval)
else
TimeBetween:=retval;
end;
不能SQL的
1秒=1其他按比例算
a=2002-04-26 23:00
b=2002-05-01 8:05
b-a
a,b:Tdatetime;
temp:double;
begin
a:=strtodatetime('2002-04-26 23:00');
b:=strtodatetime('2002-05-01 8:05'); temp:=b-a;
edit1.text:=floattostr(temp);得到。
4.37847222221899天。
var
a,b:Tdatetime;
temp:integer;
begin
a:=strtodatetime('2002-04-26 23:00');
b:=strtodatetime('2002-05-01 8:05'); temp:=round((b-a)*24*60*60);
edit1.text:=inttostr(temp);
end;得到378300(秒)这下大家满足了吧~!