如何精确的计算时间?

chinestercn 2008-04-21 12:10:09
程序中需要用到 时间计算。来表示一个动作经过的时间。

以下Function 运行算是正常。。
不过如果 系统时间: 23:59 当跳到00:00 时。 返回的值就会很大~~~~~

这个如何避免呢? 除了在Function 里判断是否为00:00 之类的方法

function js(t1,t2:TDateTime):integer; //时间差计算。返回秒数
var
h,m,s,x:word;
t3:tdatetime;
begin
t3:=t2-t1;
decodetime(t3,h,m,s,x);
result:=h*3600+m*60+s;
end;
...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
支持搂主,收藏
knowledge_Is_Life 2008-04-30
  • 打赏
  • 举报
回复
接分是王道!
手指风 2008-04-21
  • 打赏
  • 举报
回复
gettickcount函数返回计算机启动至函数调用时经过的毫秒数,经过2次调用这个函数,进行相减可以准确计算一个过程等消耗的计算机时间。
skylkj 2008-04-21
  • 打赏
  • 举报
回复
我按楼主的方法用的

t1:=strtodatetime('2008-02-01 23:59:00');
t2:=strtodatetime('2008-02-02 00:00:00');

t3:=t2-t1;
decodetime(t3,h,m,s,x);


出来的结果完全正常啊.....
dulei115 2008-04-21
  • 打赏
  • 举报
回复
下面这个Delphi自带的函数就可以实现LZ的需求
Returns the number of seconds between two specified TDateTime values.

Unit
DateUtils

function SecondsBetween(const ANow, AThen: TDateTime): Int64;
僵哥 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 chinestercn 的帖子:]
程序中需要用到 时间计算。来表示一个动作经过的时间。

以下Function 运行算是正常。。
不过如果 系统时间: 23:59 当跳到00:00 时。 返回的值就会很大~~~~~

这个如何避免呢? 除了在Function 里判断是否为00:00 之类的方法

Delphi(Pascal) code
function js(t1,t2:TDateTime):integer; //时间差计算。返回秒数
var
h,m,s,x:word;
t3:tdatetime;
begin
t3:=t2-t1;
decodetime(t3,h,m,s,x);
resul…
[/Quote]
请注意t1,t2当初始时间的时候,有没包含日期,如果有的话就到有这种问题.也不需要decodetime,直接
Result := (t2-t1) * 24(*小时*) * 60(*分*) * 60(*秒*);
genispan 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fenger8293 的回复:]
gettickcount函数返回计算机启动至函数调用时经过的毫秒数,经过2次调用这个函数,进行相减可以准确计算一个过程等消耗的计算机时间。
[/Quote]

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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