«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

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