gpt4 book ai didi

delphi - 如何正确获取两个日期之间传递的日期和时间?

转载 作者:行者123 更新时间:2023-12-03 18:13:51 29 4
gpt4 key购买 nike

我正在创建这个函数:

function LiteralTimePassed(FromDate: TDateTime; ToDate: TDateTime = 0): string;
const
Coma = ', ';
var
Dt, Mt: Integer; { se dos dias contarem mais que 30/31 então aumenta o mês }
P: TDateTime;
HC: Boolean; { indica se já há um token antes do novo token, para colocar vírgula }
Token: string; { a parte do timestamp verificada no loop }
Literal: string;
Y, M, D, H, N, S: Integer; { ano, mês, dia, hora, minuto(n), segundo }
Ys, Ms, Ds, Hs, Ns, Ss: Boolean; { valida se valores maiores que 1 para adicionar 's' }
begin
{ retorna quanto tempo se passou desde a data 1 até a data 2 em forma literal }
if ToDate = 0 then ToDate := Now;
HC := False;
Literal := '';
P := ToDate-FromDate ;
Dt := (DaysInMonth(FromDate)-DayOf(FromDate))+(DaysInMonth(ToDate)-DayOf(ToDate));
Mt := Dt div DaysInMonth(ToDate);
Ys := VarAssign(Y, YearsBetween(ToDate, FromDate)) > 1;
Ms := VarAssign(M, (MonthsBetween(ToDate, FromDate)-(Y*MonthsPerYear))-Mt) > 1;
Ds := VarAssign(D, Dt mod DaysInMonth(ToDate)) > 1;
// I did not make the hour, minute and second, yet...
end;

要获得如下响应:

 There has been "2 years, 4 months, 1 day, 7 hours, 30 minutes and 22 seconds" between those dates

我怀疑我的逻辑在计算经过天数的情况下是否正确,以及我是否必须对时间部分进行相同的计算。

但如果您知道任何已经做到这一点的免费代码,它会节省我很多时间!

谢谢。

最佳答案

先处理年和月,再处理TTimeSpan:

procedure TimePassed(dt1,dt2: TDateTime);
var
y1,m1,d1,h1,mi1,s1,ms,y2,m2,d2,h2,mi2,s2,y,mo,d:word;
ts:TTimeSpan;
begin
DecodeDateTime(dt1,y1,m1,d1,h1,mi1,s1,ms);
DecodeDateTime(dt2,y2,m2,d2,h2,mi2,s2,ms);
ms:=12*y2+m2-12*y1-m1;
if s1+60*mi1+60*60*h1+24*60*60*d1>s2+60*mi2+60*60*h2+24*60*60*d2 then ms:=ms-1;
mo:= ms mod 12;
y:=ms div 12; //years and months ready, now the rest
dt1:=EncodeDateTime(y1+y,m1+mo,d1,h1,mi1,s1,0);
ts := TTimeSpan.Subtract(dt2, dt1);
Result:= Format('There has been "%d years, %d months, %d days, %d hours, %d minutes and %d seconds" between those dates',
[y,mo,ts.Days, ts.Hours, ts.Minutes, ts.Seconds]);
end;

关于delphi - 如何正确获取两个日期之间传递的日期和时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701392/

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