如何在Delphi中格式化Unix时间戳?

如何在Delphi中格式化Unix时间戳?,delphi,Delphi,我有 我在一些MySql中使用它作为主键 但我也希望格式化日期/时间,就像我们一样 有人有代码片段或URL吗?您正在寻找的 var timestamp: Longint; timestamp := Round((Now() - 25569.0 {Unix start date in Delphi terms} ) * 86400); 及 DateUtils.pas中的函数 TDateTime值可以通过函数进行格式化这要快得多 function UnixToDateTime(const AV

我有

我在一些MySql中使用它作为主键

但我也希望格式化日期/时间,就像我们一样

有人有代码片段或URL吗?

您正在寻找的

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

DateUtils.pas中的函数

TDateTime值可以通过函数进行格式化

这要快得多

function UnixToDateTime(const AValue: Int64): TDateTime;

我会使用@kludg建议的
DateTimeToUnix

// 4x faster than dateutils version
function UNIXTimeToDateTimeFAST(UnixTime: LongWord): TDateTime;
begin
Result := (UnixTime / 86400) + 25569;
end;

// 10x faster than dateutils version
function DateTimeToUNIXTimeFAST(DelphiTime : TDateTime): LongWord;
begin
Result := Round((DelphiTime - 25569) * 86400);
end;
如果希望当前Unix时间戳采用毫秒格式,可以实现以下功能:

function DateTimeToUnix(const AValue: TDateTime): Int64;

+谢谢。我不经常这样做,但这是很有用的,请注意DateTimeToUNIXTimeFAST与DateUtils版本略有不同,如果毫秒=500..999,DateUtils将向后取整,而FAST版本将在下一秒取整。结果:=Trunc((DelphiTime-25569)*86400);包括
SysUtils
单元,您可以编写与
(UnixTime/SecsPerDay)+UnixDateDelta
圆形((DelphiTime-UnixDateDelta)*SecsPerDay)
相同的单元。如何在毫秒内获得输出?!
function DateTimeToUnix(const AValue: TDateTime): Int64;
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;