如何得到一个日期两年以后的日期?(10分)

  • 主题发起人 主题发起人 Ekin
  • 开始时间 开始时间
E

Ekin

Unregistered / Unconfirmed
GUEST, unregistred user!
如何得到一个日期两年以后的日期(select dateadd(year,2,getdate())),但是好像在碰到闰年后就不好使了!用decodedate拆分后年加2也再encodedate,如何解决2月的28.29天的问题。
 
记得加入DateUtils单元
function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer = 1): TDateTime;


Description
IncYear returns the value of the AValue parameter, incremented by ANumberOfYears years. ANumberOfYears can be negative, to return a date N years previous.

The time of day specified by the AValue parameter is copied to the result.
 
你的日期+730就是了
 
例子给你,分给我
procedure TForm1.Button1Click(Sender: TObject);
var
tmpDate: TDate;
begin
tmpDate := EncodeDate(2008, 2, 29);
IncYear(tmpDate, 2);
ShowMessage(DateToStr(IncYear(tmpDate, 2)));
end;
 
to:网中戏
这样要是碰见366天的这么办?
to:muhx 分不是问题,可以再加。
你的做法和我问题中说的第二种方法没有区别!
 
直接用sql没有什么问题啊,select dateadd(year,2,'2008-02-29')得到的是28号,
 
我的例子主要给你演示IncYear函数的使用啊
闰年的2月29号经过IncYear后变成非闰年的28号了
应该符合你的要求了
你仔细看看我的方法和你第二个方法的区别
如果EncodeDate(2008 + 2, 2, 29)那么就会报错。
在Delphi的源代码中使用了两个数组常量分别保存闰年和非闰年每个月的天数

你自己写个例子使用IncYear试一下吧
 
我已经试过了,闰年的2月29号加4年
比如tmpLeapYear是2008年2月29日那么
IncYear(tmpLeapYear, 4)得到的是2012年的2月29号

应该可以满足你的要求
 
完全同意mubx的用法,IncYear函数应该已经考虑过闰年等大家能想到的影响结果的因素了,具体可以看看代码自己试下
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
947
DelphiTeacher的专栏
D
D
回复
0
查看
897
DelphiTeacher的专栏
D
后退
顶部