如何获得经过时间格式化吗? [英] How to get elapsed time & format it?

查看:92
本文介绍了如何获得经过时间格式化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个unix时间戳作为LONG INT。我想从末尾减去以获取经过的时间并将其格式化为hh:mm:ss

I have two unix timestamps as LONG INT. I want to subtract start from end to get elapsed time and format it to hh:mm:ss

我该怎么做?谢谢

推荐答案

您可以使用 UnixToDateTime FormatDateTime 函数参见此示例

you can use the UnixToDateTime and the FormatDateTime functions see this sample

uses
  DateUtils,
  SysUtils;

var
  StartUnixTime : Int64;
  EndUnixTime   : Int64;

  StartDateTime : TDateTime;
  EndDateTime   : TDateTime;
begin
  try
    StartUnixTime:=1293062827;
    EndUnixTime  :=1293070000;

    //option 1 converting both unix times to TDatetime and then subtract
    StartDateTime:=UnixToDateTime(StartUnixTime);
    EndDateTime  :=UnixToDateTime(EndUnixTime);    
    Writeln(Format('Elapsed time %s',[FormatDateTime('hh:nn:ss',EndDateTime-StartDateTime)]));

    //option 2 subtract directly and then convert to TDatetime
    Writeln(Format('Elapsed time %s',[FormatDateTime('hh:nn:ss',UnixToDateTime(EndUnixTime-StartUnixTime))]));

  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
  Readln;
end.

另外,如果您想获取年,月和日,则可以使用 YearsBetween MonthsBetween DaysBetween 就是这种方式。

Additionally if you wanna get the Years, Months and Days , you can use the YearsBetween, MonthsBetween and the DaysBetween functions in this way.

Writeln(Format('Years %d Months %d Days %d',[YearsBetween(EndDateTime,StartDateTime),MonthsBetween(EndDateTime,StartDateTime),DaysBetween(EndDateTime,StartDateTime)]));

这篇关于如何获得经过时间格式化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆