» Help

How to convert TDateTime to a UNIX timestamp and vice versa

   

Databases in general require UNIX timestamps when reading/writing a time field. TDateTime is specific to Object Pascal. What if you want to save a date value in your applications database? Well, convertions (back and forth) are quite simple. The code is included below.

const
  UnixStartDate: TDateTime = 25569.0; // 01/01/1970

function DateTimeToUnix(dtDate: TDateTime): Longint;
begin
  Result := Round((dtDate - UnixStartDate) * 86400);
end;

function UnixToDateTime(USec: Longint): TDateTime;
begin
  Result := (Usec / 86400) + UnixStartDate;
end;
Author: Unknown
Added: 2007-06-02
Last updated: 2013-10-12

« Return to contents »