2个时间的差值(200分)(200分)

  • 主题发起人 主题发起人 灵成
  • 开始时间 开始时间

灵成

Unregistered / Unconfirmed
GUEST, unregistred user!
比如下面两个时间
2006-12-24 12:13:21
2007-11-12 18:23:21
求计算差值的详细方法,类似于倒计时
结果为离2007-11-12 18:23:21 还有 ?天,?时?分?秒
用过直接减的方法,似乎并不行,求详细代码
 
SecondsBetween
 
时间分解函数和日期分解函数
 
天数为DaysBetween,应该可以类推
 
可以:
procedure TForm1.Button4Click(Sender: TObject);
var D1,D2:TDateTime;
S:String;
i,iDay,iHour,iMinute,iSecond:Integer;
begin
D1:=strtodatetime('2007-10-24 12:13:21');
D2:=strtodatetime('2007-11-12 10:23:21');
iDay:=DaysBetween(D1,D2);
D1:=IncDay(D1,iDay);
iHour:=HoursBetween(D1,D2);
D1:=IncHour(D1,iHour);
iMinute:=MinutesBetween(D1,D2);
D1:=IncMinute(D1,iMinute);
iSecond:=SecondsBetween(D1,D2);
S:='相差'+inttostr(iDay)+'天';
S:=S+inttostr(iHour)+'小时';
S:=S+inttostr(iMinute)+'分';
S:=S+inttostr(iSecond)+'秒';
showmessage(S);
end;
 
yearsBetween
 
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label1.Caption := IntToStr(DaysBetween(Now, StrToDateTime('2007/11/12 18:23:21'))) + '天' +
IntToStr(HoursBetween(Now, StrToDateTime('2007/11/12 18:23:21')) mod 24) + '小時' +
IntToStr(MinutesBetween(Now, StrToDateTime('2007/11/12 18:23:21')) mod (24)) + '分' +
IntToStr(SecondsBetween(Now, StrToDateTime('2007/11/12 18:23:21')) mod (24)) + '秒';
Label1.Refresh;
end;
 
[Error] Unit1.pas(32): Undeclared identifier: 'DaysBetween'

什么原因,DaysBetween用不了
 
uses
DateUtils
 
需要应用
uses DateUtils;
 
DaysBetween,HoursBetween,MinutesBetween,SecondsBetween
 
加上
uses DateUtils;
 
两个日期相减怎么不行?
----------------------------
var
d1,d2,dt:TDatetime;
begin
d1:=now;
d2:=strToDatetime('2007-10-11 15:23:21');

dt:=d2-d1;

Showmessage('离'
+ FormatDatetime('yyyy-mm-dd hh:nn:ss',d2)
+ ' 还有'
+ inttostr(Trunc(dt))
+ '天 '+ FormatDatetime('hh小时 nn分钟 ss秒',dt) );
 
办法果然行啊
又学了一招
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,DateUtils;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var D1,D2:TDateTime;
S:String;
iDay,iHour,iMinute,iSecond:Integer;
begin
D1:=strtodatetime('2007-10-24 12:13:21');
D2:=strtodatetime('2007-11-12 10:23:21');
iDay:=DaysBetween(D1,D2);
D1:=IncDay(D1,iDay);
iHour:=HoursBetween(D1,D2);
D1:=IncHour(D1,iHour);
iMinute:=MinutesBetween(D1,D2);
D1:=IncMinute(D1,iMinute);
iSecond:=SecondsBetween(D1,D2);
S:='相差'+inttostr(iDay)+'天';
S:=S+inttostr(iHour)+'小时';
S:=S+inttostr(iMinute)+'分';
S:=S+inttostr(iSecond)+'秒';
showmessage(S);
edit1.Text:=s;
end;


end.

结果:相差18天22小时10分0秒
 
直接相减
 
delphi 6 附录了datetime 的大部分函数,看看就明白
 
直接减就可以,
var t1,t2:tdatetime;
t0:tdatetime;
t0:=t1-t2;
int(t0)...对应天数,
t0:=t0-int(t0);
formatdatetime('hh:mm:ss',t0);时分秒
 
可以直接相减啊 !!!再用formatdatetime格式一下
 
直接相减的话如果目标时间小于现在时间,得不到正确结果,如果相隔两天之间隔小于24小时,不显示天数(DaysBetween取得的值为0),如果两天之间隔大于24个小时,显示为1天多少小时(DaysBetween取得的值还是为0),接收了大家的意见,自己写了段函数,解决了很多问题,我的表达能力不是太好,大家自己试试就明白了。计算出的时间很精确。

Function DateTimeJIS(DateTarget,TimeTarget:TDateTime) : String;
{时间日期差计算函数,作者:灵成}
Var
ReTime,ReDay:String;
Begin
If TimeTarget < Time Then
begin
ReDay:=IntToStr(DaysBetween(Now,DateTarget));
ReTime:=TimeToStr(StrToTime('23:00:00')-(Time-TimeTarget)+StrToTime('1:00:00'));
If StrToInt(ReDay)=0 Then
Result:=ReTime
Else
Result:=ReDay+'天,'+ReTime;
End
Else
Begin
ReTime:=TimeToStr(TimeTarget-Time);
If DateTarget > Date Then
ReDay:=IntToStr(DaysBetween(Now,DateTarget)+1)
Else
ReDay:=IntToStr(DaysBetween(Now,DateTarget));
If StrToInt(ReDay)=0 Then
Result:=ReTime
Else
Result:=ReDay+'天,'+ReTime;
End;
End;
 

Similar threads

D
回复
0
查看
928
DelphiTeacher的专栏
D
D
回复
0
查看
883
DelphiTeacher的专栏
D
D
回复
0
查看
856
DelphiTeacher的专栏
D
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
916
SUNSTONE的Delphi笔记
S
后退
顶部