关于Hoursbetween函数

zhourongbiao 2005-07-21 09:48:07
下面的代码应该输出1吧?可是输出了0!
help里说
HoursBetween reports the difference between 9:00 AM and 9:59:59 AM as 0 because the difference is one second short of an entire hour.
可是好像只对起始时间非0的有效。

uses DateUtils;

procedure TForm1.Button1Click(Sender: TObject);
var
dtStart, dtEnd : TDateTime;
nowDate, LastDate : TDate;
ftMax, ftMin : double;
begin
dtStart := IncHour( DateOf(now), StrToInt('0') );
dtEnd := IncHour( DateOf(now), StrToInt('1') );
ShowMessage( IntToStr(Hoursbetween(dtStart,dtEnd) ))
end;
...全文
377 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bxh2dai 2005-07-22
  • 打赏
  • 举报
回复
UP
xixuemao 2005-07-22
  • 打赏
  • 举报
回复
唉,终于测试出来了,必须超过1小时才返回1呢,正好=1小时返回0;
procedure TForm1.Button1Click(Sender: TObject);
var
dtStart, dtEnd : TDateTime;
begin
dtStart := now;//IncHour( now, StrToInt('0') );
dtEnd := now+strtotime('1:00:01');//IncHour( now, StrToInt('1') );
showmessage(datetimetostr(dtstart));
showmessage(datetimetostr(dtend));

ShowMessage( IntToStr(Hoursbetween(dtStart,dtEnd)))
end;
xixuemao 2005-07-22
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
dtStart, dtEnd : TDateTime;
begin
dtStart := now;//IncHour( now, StrToInt('0') );
dtEnd := now+strtotime('2:00:00');//IncHour( now, StrToInt('1') );
showmessage(datetimetostr(dtstart));
showmessage(datetimetostr(dtend));

ShowMessage( IntToStr(Hoursbetween(dtStart,dtEnd)))
end;

你再试试这个,返回2,又对了,我更晕。差一天返回24也对了,就差1小时返回的值不对。
xixuemao 2005-07-22
  • 打赏
  • 举报
回复
ShowMessage( IntToStr(Hoursbetween(strtodatetime('2005-7-22 9:00:00'),strtodatetime('2005-7-22 9:59:59'))))

如果这样就可以返回1。晕了!
僵哥 2005-07-21
  • 打赏
  • 举报
回复
关键问题出在Trunc与浮点数。
qi7 2005-07-21
  • 打赏
  • 举报
回复
Thus, HoursBetween reports the difference between 9:00 AM and 9:59:59 AM as 0 because the difference is one second short of an entire hour.

16,747

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧