delphi的日期相加 ( 积分: 100 )

  • 主题发起人 主题发起人 ks_reny
  • 开始时间 开始时间
K

ks_reny

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手們,我現在遇到了一個問題,我想實現&nbsp;Delphi中的日期於數字相加&nbsp;生成新的日期類型,不知道如何實現,請高手們指點以下.<br>&nbsp;&nbsp;例子&nbsp;&nbsp;我想實現&nbsp;2007-08-13&nbsp;12:00:00&nbsp;於45&nbsp;的結果變為&nbsp;2007-08-13&nbsp;12:45:00
 
delhpi中日期型数据是可以直接相加减的。
 
如果是字符串,用StrToDateTime转成TDateTime类型,再相加,加完之后在用DateTimeToStr转回去
 
日期是一个Double类型的浮点数,年月日是它的整型部分,时分秒是它的小数部分。<br>var<br>&nbsp;&nbsp;a:TDateTime;<br>begin<br>&nbsp;&nbsp;a:=StrToDateTime('2007-08-13&nbsp;12:00:00');<br>&nbsp;&nbsp;a:=a&nbsp;+&nbsp;45*(1/24/60);//一天等于24小时,一小时60分钟<br>&nbsp;&nbsp;ShowMessage(FormatDateTime('yyyy-mm-dd&nbsp;hh:nn:ss',a));<br>end;<br><br>同理,如果要加多少天,你就直接a&nbsp;:=&nbsp;a&nbsp;+&nbsp;N&nbsp;即可,整数部分就是天。
 
Delphi中日期时间型就是Float型,操作可以和Float一样。整数是以天为单位的,小数部分算法就是&nbsp;1小时=1/24;1小时X分就是&nbsp;1/24&nbsp;+&nbsp;(1/24)/60*x。其它的自己算吧
 
DateUtils.pas中有相关的函数:<br>function&nbsp;IncDay(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfDays:&nbsp;Integer):&nbsp;TDateTime;<br>begin<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;AValue&nbsp;+&nbsp;ANumberOfDays;<br>end;<br><br>function&nbsp;IncHour(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfHours:&nbsp;Int64):&nbsp;TDateTime;<br>begin<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;((AValue&nbsp;*&nbsp;HoursPerDay)&nbsp;+&nbsp;ANumberOfHours)&nbsp;/&nbsp;HoursPerDay;<br>end;<br><br>function&nbsp;IncMinute(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfMinutes:&nbsp;Int64):&nbsp;TDateTime;<br>begin<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;((AValue&nbsp;*&nbsp;MinsPerDay)&nbsp;+&nbsp;ANumberOfMinutes)&nbsp;/&nbsp;MinsPerDay;<br>end;<br><br>function&nbsp;IncSecond(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfSeconds:&nbsp;Int64):&nbsp;TDateTime;<br>begin<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;((AValue&nbsp;*&nbsp;SecsPerDay)&nbsp;+&nbsp;ANumberOfSeconds)&nbsp;/&nbsp;SecsPerDay;<br>end;<br><br>function&nbsp;IncMilliSecond(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfMilliSeconds:&nbsp;Int64):&nbsp;TDateTime;<br>begin<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;((AValue&nbsp;*&nbsp;MSecsPerDay)&nbsp;+&nbsp;ANumberOfMilliSeconds)&nbsp;/&nbsp;MSecsPerDay;<br>end;
 
把秒换算成天,然后相加得到的就是你要的结果了。
 
謝謝大家了,我自己琢磨了以下,想法和crazycock一樣.[:D][:D]
 
Delphi里有现成的函数可以实现日期加减,是在DateUtils单元里的。<br>function&nbsp;IncYear(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfYears:&nbsp;Integer&nbsp;=&nbsp;1):&nbsp;TDateTime;<br>//&nbsp;function&nbsp;IncMonth&nbsp;is&nbsp;in&nbsp;SysUtils<br>function&nbsp;IncWeek(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfWeeks:&nbsp;Integer&nbsp;=&nbsp;1):&nbsp;TDateTime;<br>function&nbsp;IncDay(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfDays:&nbsp;Integer&nbsp;=&nbsp;1):&nbsp;TDateTime;<br>function&nbsp;IncHour(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfHours:&nbsp;Int64&nbsp;=&nbsp;1):&nbsp;TDateTime;<br>function&nbsp;IncMinute(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfMinutes:&nbsp;Int64&nbsp;=&nbsp;1):&nbsp;TDateTime;<br>function&nbsp;IncSecond(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfSeconds:&nbsp;Int64&nbsp;=&nbsp;1):&nbsp;TDateTime;<br>function&nbsp;IncMilliSecond(const&nbsp;AValue:&nbsp;TDateTime;<br>&nbsp;&nbsp;const&nbsp;ANumberOfMilliSeconds:&nbsp;Int64&nbsp;=&nbsp;1):&nbsp;TDateTime;
 
a:=IncMinute(a,45);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;好用。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部