
我试过这个:
var TimeDiff : Double;begin TimeDiff := Now - fileAgeEx('C:\my-file.txt'); if (TimeDiff >= 1) then Caption := FormatDateTime('dd hh:nn:ss',TimeDiff) else Caption := FormatDateTime('hh:nn:ss',TimeDiff);end; 但是(1)它不起作用,(2)我想要更好的格式化.
最终我的目标是拥有这样的东西:
>时间差< 1天==>显示这个:12:00:01
> Time Diff> = 1天==>显示:25天,12:00:01
> Time Diff> = 1年==>显示:2年,3个月,10天,12:00.01
谁知道我该怎么做?
谢谢!
解决方法 主要问题似乎是获取文件的上次修改时间.我使用以下代码:function LastWriteTime(const filename: string): TfileTime;var AttributeData: TWin32fileAttributeData;begin if not GetfileAttributesEx(PChar(filename),GetfileExInfoStandard,@AttributeData) then RaiseLastOSError; Result := AttributeData.ftLastWriteTime;end;function UTCfileTimetoSystemTime(const fileTime: TfileTime): TSystemTime;//returns equivalent time in current locality,taking account of daylight savingvar LocalfileTime: windows.TfileTime;begin windows.fileTimetoLocalfileTime(fileTime,LocalfileTime); windows.fileTimetoSystemTime(LocalfileTime,Result);end;function UTCfileTimetoDateTime(const fileTime: TfileTime): TDateTime;begin Result := SystemTimetoDateTime(UTCfileTimetoSystemTime(fileTime));end;
您调用LastWriteTime以文件时间格式获取上次修改时间.然后调用UTCfileTimetoDateTime转换为TDateTime,计算机器的主要本地时区.然后,您可以将该值与Now进行比较.
关于格式化,您似乎已经知道如何做到这一点.您的基本方法将起作用,您只需要充实细节.
在评论中你说
FormatDateTime('dd hh:nn:ss',2.9); 当你想要2时显示1.问题是这个函数格式化日期而不是时间间隔.值2.9不被视为经过时间,而是被视为绝对日期/时间,即德尔福纪元后2.9天.我会使用Trunc和Frac分别获得天数和天数,并从那里开始工作.
Days := Trunc(TimeDiff);Time := Frac(TimeDiff);
以下代码直接从我的代码库中提取,可能会给你一些指示.请注意,它的输入是以秒为单位,但它应该设置在正确的路径上.
function CorrectPlural(const s: string; Count: Integer): string;begin Result := IntToStr(Count) + ' ' + s; if Count<>1 then begin Result := Result + 's'; end;end;function HumanReadableTime(Time: Double): string;//Time is in secondsconst SecondsPerMinute = 60; SecondsPerHour = 60*SecondsPerMinute; SecondsPerDay = 24*SecondsPerHour; SecondsPerWeek = 7*SecondsPerDay; SecondsPerYear = 365*SecondsPerDay;var Years,Weeks,Days,Hours,Minutes,Seconds: Int64;begin Try Years := Trunc(Time/SecondsPerYear); Time := Time - Years*SecondsPerYear; Weeks := Trunc(Time/SecondsPerWeek); Time := Time - Weeks*SecondsPerWeek; Days := Trunc(Time/SecondsPerDay); Time := Time - Days*SecondsPerDay; Hours := Trunc(Time/SecondsPerHour); Time := Time - Hours*SecondsPerHour; Minutes := Trunc(Time/SecondsPerMinute); Time := Time - Minutes*SecondsPerMinute; Seconds := Trunc(Time); if Years>5000 then begin Result := IntToStr(Round(Years/1000))+' millennia'; end else if Years>500 then begin Result := IntToStr(Round(Years/100))+' centurIEs'; end else if Years>0 then begin Result := CorrectPlural('year',Years) + ' ' + CorrectPlural('week',Weeks); end else if Weeks>0 then begin Result := CorrectPlural('week',Weeks) + ' ' + CorrectPlural('day',Days); end else if Days>0 then begin Result := CorrectPlural('day',Days) + ' ' + CorrectPlural('hour',Hours); end else if Hours>0 then begin Result := CorrectPlural('hour',Hours) + ' ' + CorrectPlural('minute',Minutes); end else if Minutes>0 then begin Result := CorrectPlural('minute',Minutes); end else begin Result := CorrectPlural('second',Seconds); end; Except Result := 'an eternity'; End;end; 总结 以上是内存溢出为你收集整理的Delphi中的经过/时间间隔?全部内容,希望文章能够帮你解决Delphi中的经过/时间间隔?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)