unit f_date_tools;interface
uses DateUtils,SysUtils;type T_date_tools = class
private
d_date:TDate;
public
constructor create( the_date_string:string );reintroduce;overload;
constructor create( the_date:TDate );reintroduce;overload;
destructor destroy();override; function tomorrow():T_date_tools;
function yesterday():T_date_tools; function nextSomeDays( the_days:integer ):T_date_tools;
function previousSomeDays( the_days:integer ):T_date_tools; function nextMonthToday():T_date_tools;
function previousMonthDay():T_date_tools; function toString():string;
end;implementationend.//要注意的是,就是能连续tommorow();
//例如:
// var temp_date_tools := T_date_tools.create('2006-2-14');
// var temp_date_string := temp_date_tools.tommorow().tommorow().toString();
uses DateUtils,SysUtils;type T_date_tools = class
private
d_date:TDate;
public
constructor create( the_date_string:string );reintroduce;overload;
constructor create( the_date:TDate );reintroduce;overload;
destructor destroy();override; function tomorrow():T_date_tools;
function yesterday():T_date_tools; function nextSomeDays( the_days:integer ):T_date_tools;
function previousSomeDays( the_days:integer ):T_date_tools; function nextMonthToday():T_date_tools;
function previousMonthDay():T_date_tools; function toString():string;
end;implementationend.//要注意的是,就是能连续tommorow();
//例如:
// var temp_date_tools := T_date_tools.create('2006-2-14');
// var temp_date_string := temp_date_tools.tommorow().tommorow().toString();
//////////////////////////////
type T_date_tools = class
private
d_date:TDate;
public
constructor create( the_date_string:string );reintroduce;overload;
constructor create( the_date:TDate );reintroduce;overload;
destructor destroy();override; function tomorrow():T_date_tools;
function yesterday():T_date_tools; function nextSomeDays( the_days:integer ):T_date_tools;
function previousSomeDays( the_days:integer ):T_date_tools; function nextMonthToday():T_date_tools;
function previousMonthDay():T_date_tools; function toString():string;
end;implementationconstructor T_date_tools.create( the_date_string:string );
begin
inherited Create();
d_date:=StrToDate(the_date_string);
end;constructor T_date_tools.create( the_date:TDate );
begin
inherited Create();
d_date:=the_date;
end;destructor T_date_tools.destroy();
begin
inherited Destroy;
end;function T_date_tools.tomorrow():T_date_tools;
var
tdtTemp:T_date_tools;
begin
tdtTemp:=T_date_tools.create(d_date+1);
Result:=tdtTemp;
end;function T_date_tools.yesterday():T_date_tools;
var
tdtTemp:T_date_tools;
begin
tdtTemp:=T_date_tools.create(d_date-1);
Result:=tdtTemp;
end;function T_date_tools.nextSomeDays( the_days:integer ):T_date_tools;
var
tdtTemp:T_date_tools;
begin
tdtTemp:=T_date_tools.create(d_date+the_days);
Result:=tdtTemp;
end;function T_date_tools.previousSomeDays( the_days:integer ):T_date_tools;
var
tdtTemp:T_date_tools;
begin
tdtTemp:=T_date_tools.create(d_date-the_days);
Result:=tdtTemp;
end;function T_date_tools.nextMonthToday():T_date_tools;
var
tdtTemp:T_date_tools;
begin
tdtTemp:=T_date_tools.create(incMonth(d_date));
Result:=tdtTemp;
end;function T_date_tools.previousMonthDay():T_date_tools;
var
tdtTemp:T_date_tools;
begin
tdtTemp:=T_date_tools.create(incMonth(d_date,-1));
Result:=tdtTemp;
end;function T_date_tools.toString():string;
begin
Result:=FormatDateTime('yyyy-mm-dd',d_date);
end;
begin
inherited Create();
d_date:=StrToDate(the_date_string);
end;constructor T_date_tools.create( the_date:TDate );
begin
inherited Create();
d_date:=the_date;
end;destructor T_date_tools.destroy();
begin
inherited Destroy;
end;function T_date_tools.tomorrow():T_date_tools;
begin
Result:=T_date_tools.create(d_date+1);
end;function T_date_tools.yesterday():T_date_tools;
begin
Result:=T_date_tools.create(d_date-1);
end;function T_date_tools.nextSomeDays( the_days:integer ):T_date_tools;
begin
Result:=T_date_tools.create(d_date+the_days);
end;function T_date_tools.previousSomeDays( the_days:integer ):T_date_tools;
begin
Result:=T_date_tools.create(d_date-the_days);
end;function T_date_tools.nextMonthToday():T_date_tools;
begin
Result:=T_date_tools.create(incMonth(d_date));
end;function T_date_tools.previousMonthDay():T_date_tools;
begin
Result:=T_date_tools.create(incMonth(d_date,-1));
end;function T_date_tools.toString():string;
begin
Result:=FormatDateTime('yyyy-mm-dd',d_date);
end;
begin
d_date := d_date+the_days;
Result := self;
end;function T_date_tools.tomorrow():T_date_tools;
begin
Result:=self.nextSomeDays(1);
end;
你的算法
function T_date_tools.nextSomeDays( the_days:integer ):T_date_tools;
begin
d_date := d_date+the_days;
Result := self;
end;function T_date_tools.tomorrow():T_date_tools;
begin
Result:=self.nextSomeDays(1);
end;
/////////////////
看起来好象更好,不用建立对象,但是,当程序调用时会出现问题。
var
tmp_date:T_Date_tools;
begin
tmp_date:=t_Date_tools.create(Edit1.Text);
Label1.Caption:=tmp_date.tomorrow().toString;
Label2.Caption:=tmp_date.yesterday().toString;
/////yesterday()变成了当天。应该不是这样使用的目的。类不应该改变内部的内容。