Beranda > Delphi > How to convert TDateTime to a UNIX timestamp and vice versa

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;
Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: