How to format a Unix timestamp in Delphi?

前端 未结 3 1226
[愿得一人]
[愿得一人] 2021-02-04 07:36

I have

var timestamp: Longint;  
timestamp := Round((Now() - 25569.0 {Unix start date in Delphi terms} ) * 86400);

which I am using as a prima

3条回答
  •  有刺的猬
    2021-02-04 07:54

    I would use DateTimeToUnix, as suggested by @kludg.

    function DateTimeToUnix(const AValue: TDateTime): Int64;
    

    In case you want the current Unix timestamp in milliseconds format, you can implement the following function:

    function UNIXTimeInMilliseconds: Int64;
    var
      DateTime: TDateTime;
      SystemTime: TSystemTime;
    begin
      GetSystemTime(SystemTime);
      DateTime := SysUtils.EncodeDate(SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay) +
            SysUtils.EncodeTime(SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds);
      Result := DateUtils.MilliSecondsBetween(DateTime, UnixDateDelta);
    end;
    

提交回复
热议问题