Programming C# C++ (7) Delphi (618) .NET (2) Database (71) Delphi IDE (90) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280) Java (8) JavaScript (29) perl (9) php (4) VBScript (1) Visual Basic (1)
Exchange Links About this site Links to us 
|
How to convert a Unix timestamp to the TDateTime format
1 comments. Current rating: (1 votes). Leave comments and/ or rate it.
Question:
There are various situations where you may need to convert a unix timestamp to the TDateTime format and back, e.g. in a mixed environment where you access files on a Unix system or if you use a mySQL database.
How can it be done?
Answer:
The code below uses the unix start date (1/1/1970) as a TDateTime constant and multiplies (divides) with the amount of seconds per day which is 86400.
You can call the functions like this:
DateTimeToUnix(now);
UnixToDateTime(1002187414);
 | |  | | const
UnixStartDate: TDateTime = 25569.0;
function DateTimeToUnix(ConvDate: TDateTime): Longint;
begin
Result := Round((ConvDate - UnixStartDate) * 86400);
end;
function UnixToDateTime(USec: Longint): TDateTime;
begin
Result := (Usec / 86400) + UnixStartDate;
end;
| |  | |  | You don't like the formatting? Check out SourceCoder then!
Comments:
2008-06-08, 06:23:16 (updated: 2008-06-08, 06:36:33) |
|
|
|
works well.
|
|