zoukankan      html  css  js  c++  java
  • [整理]当前日期的加减运算

    在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;
  • 相关阅读:
    UVA 1600
    P3366 【模板】最小生成树(堆优化prim)
    P2414 [NOI2011]阿狸的打字机
    P2322 [HNOI2006]最短母串问题
    P4052 [JSOI2007]文本生成器
    P4824 [USACO15FEB]Censoring (Silver) 审查(银)&&P3121 [USACO15FEB]审查(黄金)Censoring (Gold)
    P3966 [TJOI2013]单词
    P2444 [POI2000]病毒
    P3294 [SCOI2016]背单词
    P2922 [USACO08DEC]秘密消息Secret Message
  • 原文地址:https://www.cnblogs.com/go-jzg/p/4196809.html
Copyright © 2011-2022 走看看