开发者

How to remove the seconds from TTime without resorting to TimeToStr(const datetime:TDateTime; const formatsettings:TFormatSettings)

How can the seconds be removed from a TTime variable without resorting to the extra overhead of using TimeToStr(const datetime:TDateTime; const formatsettings:TFormatSettings) to get the TTime value without the seconds?

ie this is what I need -> HH:MM:00.

Is there some kind of math operation (like ANDing or O开发者_高级运维Ring the value with something) that can be performed?


uses
  ..., DateUtils;

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


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

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


var
t : TTime;

t := Trunc(t * MinsPerDay) / MinsPerDay;

EDIT :

This is a more accurate function to truncate the seconds. It rounds the time to nearest milliseconds before truncating.

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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜