Notice Of Closure

Please note that this website is due to close down on 15th June 2020.

In the interim I will try to move as much content as possible over to my GitHub account. Thanks to everyone who has supported the site over the years.

» 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.

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

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

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

« Return to contents »