zoukankan      html  css  js  c++  java
  • delphi计算两个时间差-转

    uses DateUtils;
     
    var
    S1, S2: string;
    T1, T2: TDateTime;
    D, H, M, S: Integer;
    Value: Int64;
    begin
    S1 := '2015/09/23 15:44:50';
    S2 := '2013/09/22 16:47:51';
    T1 := StrToDateTime(S1);
    T2 := StrToDateTime(S2);
     
    Value := SecondsBetween(T1, T2);
    D := Value div SecsPerDay;                                  // 取一天有多少秒 
    H := Value mod SecsPerDay div SecsPerHour;                  // 取一天有多少秒
    M := Value mod SecsPerDay mod SecsPerHour div SecsPerMin;
    S := Value mod SecsPerDay mod SecsPerHour mod SecsPerMin;
    Caption := Format('%.2d天 %.2d:%.2d:%.2d', [D, H, M, S]); //%.2d没有两位补全,若没有'.'则显示实际位数
    memo1.Lines.Add(caption);
    end;

      

    经过上面可以实现两个时间相减的功能,然后将其写成函数为:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    function GetSubDateTime(S1, S2:string): string;
    var
      T1, T2: TDateTime;
      D, H, M, S: Integer;
      Value: Int64;
    begin
      T1 := StrToDateTime(S1);
      T2 := StrToDateTime(S2);
      Value := SecondsBetween(T1, T2);
      D := Value div SecsPerDay;
      H := Value mod SecsPerDay div SecsPerHour;
      M := Value mod SecsPerDay mod SecsPerHour div SecsPerMin;
      S := Value mod SecsPerDay mod SecsPerHour mod SecsPerMin;
      result := Format('%.2d天 %.2d:%.2d:%.2d',[D, H, M, S]);
    end;<br><br>调用:<br>var<br>Caption: string;<br>begin<br>  Caption := GetSubDateTime(S1, S2);<br>  memo1.liens.add(Caption);<br>end;

    上面就可以直接调用函数计算差值,若要想计算动态的时间差值就使用一个计时器Timer,代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    procedure TForm2.Timer1Timer(Sender: TObject);
    var
      S1, S2: string;
    begin
      S1 := FormatDateTime('yyyy/mm/dd hh:mm:ss', now());   // 我用的XE,所以提前出来的系统时间是这种格式
      S2 := '2015/9/22 01:02:03';                           // 这里时间要和获取到的系统时间一致
      GetSubDateTime(S1, S2);
      Memo1.Lines.Add(GetSubDateTime(S1, S2));
    end;

      

  • 相关阅读:
    mysql命令集锦
    linux 删除文件名带括号的文件
    linux下的cron定时任务
    struts2文件下载的实现
    贴一贴自己写的文件监控代码python
    Service Unavailable on IIS6 Win2003 x64
    'style.cssText' is null or not an object
    "the current fsmo could not be contacted" when change rid role
    远程激活程序
    新浪图片病毒
  • 原文地址:https://www.cnblogs.com/maweiwei/p/12732451.html
Copyright © 2011-2022 走看看