Delphi 当前日期的加减运算


本文整理自网络,侵删。

 
在DateUtils(d7)里,有如下几个现成的函数 
function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer = 1): TDateTime; 
// function IncMonth is in SysUtils 
function IncWeek(const AValue: TDateTime; const ANumberOfWeeks: Integer = 1): TDateTime; 
function IncDay(const AValue: TDateTime; const ANumberOfDays: Integer = 1): TDateTime; 
function IncHour(const AValue: TDateTime; const ANumberOfHours: Int64 = 1): TDateTime; 
function IncMinute(const AValue: TDateTime; const ANumberOfMinutes: Int64 = 1): TDateTime; 
function IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64 = 1): TDateTime; 
function IncMilliSecond(const AValue: TDateTime; const ANumberOfMilliSeconds: Int64 = 1): TDateTime; 
你把增加的值设置成-1就可以得到前一(或N)年/月/日的日期了
扩冲例子如下: 
var 
  str:string; 
  d:TDateTime; 
begin 
  d:=now;            
  //yyyy-m-d h:n:s:zz 格式化组成:年-月-日 时:分:秒:毫秒 
  ShowMessage('当前时间:'+DateTimeToStr(d)); 

  str:=FormatDateTime('yyyy-mm-dd h:n:s:zz',d-1); //取得当前时间减一天 
  ShowMessage('当前时间减一天:'+str); 

  str:=FormatDateTime('yyyy-mm-dd h:n:s:zz',d-30); //取得当前时间减一月 
  ShowMessage('当前时间减一月:'+str); 

  str:=FormatDateTime('yyyy-mm-dd h:n:s:zz',d-1/24); //取得当前时间减一小时 
  ShowMessage('当前时间减一小时:'+str); 

  str:=FormatDateTime('yyyy-mm-dd h:n:s:zz',d-1/24/60); //取得当前时间减一分钟 
  ShowMessage('当前时间减一分钟:'+str); 

  str:=FormatDateTime('yyyy-mm-dd h:n:s:zz',d-1/24/60/1000/60); //取得当前时间减一毫秒 
  ShowMessage('当前时间减一毫秒:'+str); 
end;

相关阅读 >>

Delphi 的运算符列表

Delphi 利用 showmessage(syserrormessage(getlasterror));得到不反馈的错误信息

Delphi 辨析 field、fielddef、fields、fielddefs、fieldlist、fielddeflist

Delphi调用阿里云的对象存储服务oss

如何在Delphi中禁用关于“返回值...可能未定义”的警告?

Delphi获取文件或文件夹属性和修改属性

Delphi 颜色转换函数: 从 Delphi 到 html

Delphi app如何禁掉安卓锁屏

Delphi 创建文件夹并打开

Delphi双进程监控

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...