如何在不诉诸TimeToStr的情况下从TTime中删除秒(const datetime:TDateTime; const formatsettings:TFormatSettings)

时间:2011-06-25 04:52:28

标签: delphi

如何在不使用TimeToStr(const datetime:TDateTime; const formatsettings:TFormatSettings)来获取没有秒的TTime值的额外开销的情况下从TTime变量中删除秒数?

即这是我需要的 - > HH:MM:00

是否有某种数学运算(如ANDing或ORing值与某些东西)可以执行?

4 个答案:

答案 0 :(得分:20)

uses
  ..., DateUtils;

var
  t: TTime;
begin
  t := ...;
  t := RecodeSecond(t, 0);
end;

答案 1 :(得分:18)

var
  t: TTime;
  iHour, iMin, iSec, iMSec: Word;

DecodeTime(t, iHour, iMin, iSec, iMSec);
t := EncodeTime(iHour, iMin, 0, 0);

答案 2 :(得分:3)

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;

答案 3 :(得分:0)

LongTimeFormat="hh:mm";
Label1->Caption=TimeToStr(Time());