gpt4 book ai didi

delphi - 如何获取耗时并格式化它?

转载 作者:行者123 更新时间:2023-12-03 15:42:34 25 4
gpt4 key购买 nike

我有两个 LONG INT 类型的 Unix 时间戳。我想从结束中减去开始以获得耗时并将其格式化为 hh:mm:ss

我该怎么做?谢谢

最佳答案

您可以使用UnixToDateTimeFormatDateTime函数请参阅此示例

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 , MonthsBetweenDaysBetween以这种方式发挥作用。

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

关于delphi - 如何获取耗时并格式化它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4515544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com