求两个Tdatetime类型时间差的函数?(50分)

  • 主题发起人 scorpions
  • 开始时间
S

scorpions

Unregistered / Unconfirmed
GUEST, unregistred user!
求两个Tdatetime类型时间差的函数?
var
a,b:Tdatetime;
begin
a:=strtodatetime('2002-6-7 12:12:12');
b:=strtodatetime('2002-6-7 12:13:13');
edit1.Text :=datetimetostr(b-a);
结果居然是:1899-12-30 0:01:01 日期差不对。
 
可以用date/time routines来做
比如DaysBetween function,MinutesBetween function等函数,查找一下delphi的帮助吧,你
会有收获的
 
procedure TForm1.Button1Click(Sender: TObject);
var
a,b:Tdatetime;
year,month,day,hour,minute,second,msecond:word;
year1,month1,day1,hour1,minute1,second1,msecond1:word;
begin
a:=strtodatetime('2002-6-7 12:12:12');
b:=strtodatetime('2002-6-7 12:13:13');
decodedate(a,year,month,day);
decodetime(a,hour,minute,second,msecond);
decodetime(b,hour1,minute1,second1,msecond1);
decodedate(b,year1,month1,day1);
edit1.text:=intTostr(year1-year)+'年'+intTostr(month1-month)+'月'+intTostr(day1-day)+'日'+intTostr(hour1-hour)+'时'+intTostr(minute1-minute)+'分'+intTostr(second1-second)+'秒'+IntToStr(Msecond1-msecond)+'毫秒';
end;
===================================
下面有补充。
 
结果很对啊,怎么不对?
OLE标准中,日期型变量是个浮点数,0表示1899-12-30 00:00:00
整数部分表示年月日,小数部分表示时分秒,以此类推
两个日期相减,得出1分1秒
datetimetostr一下,当然就是1899-12-30 0:01:01
再对也没有了。
这个问题回答过好多遍了,Delphi的help里也有
 
看看这个帖子。答案就在里面。
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1131624
 
版猪,你求的是日期、时间的差值,转换回日期时间类型是什么意思?
是不是在理解上有出入?
参考下左轻侯的解答,正如其所述!

a:=strtodatetime('2002-6-7 12:12:12');
b:=strtodatetime('2002-6-7 12:13:13');
edit1.Text :=floattostr(b-a);
edit1.Text就是时间差

 
procedure TForm1.Button1Click(Sender: TObject);
var
a,b:Tdatetime;
year,month,day,hour,minute,second,msecond:word;
year1,month1,day1,hour1,minute1,second1,msecond1:word;
begin
a:=strtodatetime('2002-6-12 12:12:12');
b:=strtodatetime('2002-7-7 12:13:13');
decodedate(a,year,month,day);
decodetime(a,hour,minute,second,msecond);
decodetime(b,hour1,minute1,second1,msecond1);
decodedate(b,year1,month1,day1);
if msecond1<msecond then
begin
msecond1:=msecond1+1000;
second1:=second1-1;
end;
if second1<second then
begin
second1:=second1+60;
minute1:=minute1-1;
end;
if minute1<minute then
begin
minute1:=minute1+60;
hour1:=hour1-1;
end;
if hour1<hour then
begin
hour1:=hour1+24;
day1:=day1-1;
end;
if day1<day then
begin
if month1 in [1,3,5,7,8,10,12] then
day1:=day1+31
else if month1 in [4,6,9,11] then
day1:=day1+30
else if month1 = 2 then
begin
if year1 div 4 =year1/4 then
day1:=day1+29
else
day1:=day1+28;
end;
month1:=Month1-1;
end;
if month1<month then
begin
month1:=month1+12;
year1:=year1-1;
end;
if year1<year then
ShowMessage('得到的结果为负数!');
edit1.text:=intTostr(year1-year)+'年'+intTostr(month1-month)+'月'+intTostr(day1-day)+'日'+intTostr(hour1-hour)+'时'+intTostr(minute1-minute)+'分'+intTostr(second1-second)+'秒'+IntToStr(Msecond1-msecond)+'毫秒';
end;


end.
 
接受答案了.
 
顶部