to lining_1977(ln) :如何直接相减? to cll007(gazo)和hhzqf1980(hh) :我用了,但是得到的数值有时候是错误的,比如: a:=inttostr(DaysBetween(2001-1-3,2004-4-1));那么a得到的是2。 我看到有的资料说DaysBetween(now(),StrToDateTime('2003-10-31')); 便返回相隔的天数 是吗? to wywry:谢谢,如果直接用代码不能实现,就用控件了
Delphi Object and Component Reference DaysBetween functionSee alsoReturns the number of whole days between two specified TDateTime values.UnitDateUtilsCategorydatetime routinesDelphi syntax:function DaysBetween(const ANow, AThen: TDateTime): Integer;C++ syntax:extern PACKAGE int __fastcall DaysBetween(const System::TDateTime ANow, const System::TDateTime AThen);DescriptionCall DaysBetween to obtain the difference, in days, between two TDateTime values.DaysBetween counts only whole days. Thus, DaysBetween reports the difference between Dec 31, 1999 11:59 PM and Jan 1, 2000 11:58 PM as 0 because the difference is one minute short of an entire day.
procedure TForm1.Button1Click(Sender: TObject); var a,b : TDateTime; //TDateTime变量,本质上是一个浮点类型, //它的整数部分表示天,小数部分表示1天中的时间 //也就是说1表示1天,0.5表示12小时。 //计算的日期从1899-12-31 0:0:0开始计算,也就是0 //这天以后是正数,以前的是负数 //这回明白了么? begin a := strtodate('2004-01-01'); b := now; Caption := format('%f',[(b-a)]); end;
daysBetween(d1,d2)
date1,date2:string;
i:integer;
begin
date1:='2004-4-5';
date2:='2009-9-1';
edit1.text:=inttostr(strtodate(date1)-strtodate(date2));
end;
但是要引用 :DateUtils
to cll007(gazo)和hhzqf1980(hh) :我用了,但是得到的数值有时候是错误的,比如:
a:=inttostr(DaysBetween(2001-1-3,2004-4-1));那么a得到的是2。
我看到有的资料说DaysBetween(now(),StrToDateTime('2003-10-31')); 便返回相隔的天数
是吗?
to wywry:谢谢,如果直接用代码不能实现,就用控件了
请详细说明哈,好吗?
a:=inttostr(DaysBetween(2001-1-3,2004-4-1));那么a得到的是2。
为什么?
DaysBetween functionSee alsoReturns the number of whole days between two specified TDateTime values.UnitDateUtilsCategorydatetime routinesDelphi syntax:function DaysBetween(const ANow, AThen: TDateTime): Integer;C++ syntax:extern PACKAGE int __fastcall DaysBetween(const System::TDateTime ANow, const System::TDateTime AThen);DescriptionCall DaysBetween to obtain the difference, in days, between two TDateTime values.DaysBetween counts only whole days. Thus, DaysBetween reports the difference between Dec 31, 1999 11:59 PM and Jan 1, 2000 11:58 PM as 0 because the difference is one minute short of an entire day.
var
a,b : TDateTime;
//TDateTime变量,本质上是一个浮点类型,
//它的整数部分表示天,小数部分表示1天中的时间
//也就是说1表示1天,0.5表示12小时。
//计算的日期从1899-12-31 0:0:0开始计算,也就是0
//这天以后是正数,以前的是负数
//这回明白了么?
begin
a := strtodate('2004-01-01');
b := now;
Caption := format('%f',[(b-a)]);
end;
而SQL Server中的0才是1899-12-31 0:0:0
呵呵,这个看不明白,你自己试试就知道了,
先用这个试:
Caption := Datetimetostr(-0.6);
再试这个
Caption := Datetimetostr(-1.6);
再试这个
Caption := Datetimetostr(-2.6);
TDateTime变量,就是一个浮点类型,整数代表天数,小数代表小时。都是以现在的时间减去1899-12-31 0:0:0。