delphi 日期时间计算


本文整理自网络,侵删。

 一、

  TDateTime类型怎么进行加减运算?比如我要在当前的日期时间上再加30分钟,得到一个新的日期时间,应该怎么运算?

答: TDateTime类型可直接进行运算

 

(*前n秒的TDateTime是多少?
   前n分钟的TDateTime是多少?
   前n小时的TDateTime是多少?
   前n天的TDateTime是多少?
   前n周的TDateTime是多少?

   答案:
   now-n/3600/24
   now-n/60/24
   now-n/24
   now-n*1
   now-n*7   *)

 

   Memo1.Clear;
   Memo1.Lines.Add('当前时间:' + DatetimeToStr(Now));
   Memo1.Lines.Add('前20秒的TDateTime是多少?' + DatetimeToStr(Now-20/3600/24));
   Memo1.Lines.Add('前2分钟的TDateTime是多少?' + DatetimeToStr(Now-2/60/24));
   Memo1.Lines.Add('前2小时的TDateTime是多少?' + DatetimeToStr(Now-2/24));
   Memo1.Lines.Add('前2天的TDateTime是多少?' + DatetimeToStr(Now-2*1));
   Memo1.Lines.Add('前2周的TDateTime是多少?' + DatetimeToStr(Now-2*7));

运行结果:

当前时间:2011-12-9 下午 03:39:06
前20秒的TDateTime是多少?2011-12-9 下午 03:38:46
前2分钟的TDateTime是多少?2011-12-9 下午 03:37:06
前2小时的TDateTime是多少?2011-12-9 下午 01:39:06
前2天的TDateTime是多少?2011-12-7 下午 03:39:06
前2周的TDateTime是多少?2011-11-25 下午 03:39:06

 

二、

有控件   DateTimePicker1   , 
假如   DateTimePicker1   值为 2004-7-5   ,   
请问:如何求出  
1.本月第一日(   即2004-7-1   ), 
2.本月最后一日(   即2004-7-31   ), 

3.上个月当日   (   即2004-6-5   ), 
4.上个月最后一日(   即2004-6-30   ), 

5.上年当月当日(   即2003-7-5   ). 
6.上年上月当日(   即2003-6-5   ). 
7.上年上月最后一日(   即2003-6-30   ). 

最头痛的问题是各个月的天数不同,还要考虑是否闰年的“二月份”!

DataUtils.unit....你所有的   都有。。

Var 
    Year1,Month1,Day1:   Word; 
begin 
    DecodeDate(DateTimePicker1.date,year1,month1,day1); 
    Datetimepicker1.Date:=   EncodeDAte(Year1,Month1,1);     //本月第一天 
end;
var 
    Y,M,D:Word; 
    NewDate:TDateTime; 
begin 
    DecodeDate(DateTime,   Y,   M,   D); 
    NewDate:= 
    1.本月第一日(   即2004-7-1   ),   EncodeDate(Y,M,1) 
    2.本月最后一日(   即2004-7-31   ),   EncodeDate(Y,M+1,1)-1 
    (就是下个月1号的前一天) 
3.上个月当日   (   即2004-6-5   ),EncodeDate(Y,M-1,D) 
4.上个月最后一日(   即2004-6-30   ),EncodeDate(Y,M,1)-1 

5.上年当月当日(   即2003-7-5   ).EncodeDate(Y-1,M,1) 
6.上年上月当日(   即2003-6-5   ).EncodeDate(Y-1,M-1,D) 
7.上年上月最后一日(   即2003-6-30   ).EncodeDate(Y-1,M,1)-1
如果碰到求1月份的上一个月怎么改? 

2004-1-05   的上个月当日(   即2003-12-05). 

EncodeDate(Y,M-1,D);   //这个做法会出错哦 


 

三、

delphi中使用datetimepicker,怎么进行HH,MM,SS的加减计算?

delphi 中DateUtils单元有详细的年、月、日、时、分、秒、星期等时间加减函数,如下:

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; 

举例:

比如, 分钟相加 的话 使用 IncMinute,加30分钟分钟
   edit1.text := DatetimetoStr(IncMinute(datetimepicker.time,30));
以此类推。。

相关阅读 >>

Delphi 调用批处理

Delphi 获取系统日期时间

Delphi窗口随机类名

Delphi tfile

Delphi fmx 切换窗体最大化

Delphi中format函数的用法

Delphi 如何将bitmap位图与base64字符串相互转换

Delphi获取jpg图片的高度、宽度

Delphi 获取网卡mac为软件注册机器码

快速查询posex与posrightex

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



打赏

取消

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

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

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

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

评论

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