问题描述
|
如何从TTime变量中删除秒,而又不使用使用TimetoStr(const datetime:TDateTime; const formatsettings:TFormatSettings)来获得没有秒的TTime值的额外开销?
即这就是我需要的-> HH:MM:00。
是否可以执行某种数学运算(例如将值与或进行或运算)?
解决方法
var
t: TTime;
iHour,iMin,iSec,iMSec: Word;
DecodeTime(t,iHour,iMSec);
t := EncodeTime(iHour,0);
, uses
...,DateUtils;
var
t: TTime;
begin
t := ...;
t := RecodeSecond(t,0);
end;
, var
t : TTime;
t := Trunc(t * MinsPerDay) / MinsPerDay;
编辑:
这是截断秒数的更准确的功能。
在舍入之前,它会将时间舍入到最接近的毫秒。
uses
SysUtils;
const
FMSecsPerDay: Double = MSecsPerDay;
FMSecsPerMinute: Double = SecsPerMin * MSecsPerSec;
FMinsPerDay: Double = HoursPerDay * MinsPerHour;
function TruncateSeconds(aTime: TDateTime): TDateTime;
begin
{ - Round to closest millisecond before truncating the seconds }
Result := Trunc(Round(aTime * FMSecsPerDay) / FMSecsPerMinute) / FMinsPerDay;
// -- Time in Milliseconds --
// ------------- Time in minutes -----------------------
end;
, LongTimeFormat=\"hh:mm\";
Label1->Caption=TimeToStr(Time());