How works the date format in Delphi
This is fully documented on Embarcadero's DocWiki:
System.TDateTime
The TDateTime
class inherits a val data member--declared as a double--that holds the date-time value. The integral part of a TDateTime
value is the number of days that have passed since December 30, 1899. The fractional part of a TDateTime
value is the time of day.
...
The following table displays examples of TDateTime
values and their corresponding dates and times:
Value Description
0 December 30, 1899; 12:00 A.M.
2.75 January 1, 1900; 6:00 P.M.
-1.25 December 29, 1899; 6:00 A.M.
35065 January 1, 1996; 12:00 A.M.
how convert a unix date to delphi date with math?
Side note: i can't modify the server, i need to solve the issue client side with javascript
A Unix date/time is represented as the number of seconds since January 1 1970 00:00:00 UTC. Delphi has a UnixDateDelta
constant in the SysUtils
unit which is defined as 25569
, the number of days from December 31 1899 to January 1 1970. So, a TDateTime
value of 25569.0
exactly represents January 1 1970 00:00:00 (UTC vs local is up to you to decide when creating a TDateTime
). You can then add seconds to that value to get the final TDateTime
value for any Unix date/time.
In a TDateTime
, you can add whole days to the integral portion (ie, Unix + 1 day = 25569.0 + 1 = 25570.0
), but adding seconds within a day is slightly more work, as seconds are not represented as-is in TDateTime
, as you can see in the table above. 0.25
is 6:00 AM (21600 seconds after midnight) and 0.75
is 6:00 PM (64800 seconds after midnight). So seconds are represented in TDateTime
as a fraction with 86400 (the number of seconds in a day) as the denominator.
A JavaScript Date
object is represented as the number of milliseconds since midnight on January 1 1970. You can divide a Date
value by 1000 to get whole seconds, and divide that value by 86400 to get whole days and fractional seconds, which you can then add to 25569.0
to produce a TDateTime
value.
function jsDateToDelphiDate(dateToConvert){
const UnixDateDelta = 25569.0;
const SecsPerDay = 86400;
const MSecsPerSec = 1000;
var UnixSeconds = dateToConvert.getTime() / MSecsPerSec; // 1563984000
var SecsToAdd = UnixSeconds / SecsPerDay; // 18101.666666666668
return UnixDateDelta + SecsToAdd;
}
// don't forget to force UTC, or else the Date value
// will be skewed by the local timezone offset...
console.log(jsDateToDelphiDate(new Date("2019-07-24T16:00:00Z"))); // 43670.66666666667
console.log(jsDateToDelphiDate(new Date(Date.UTC(2019, 6, 24, 16, 0, 0)))); // 43670.66666666667
Delphi has a UnixToDateTime()
function in the DateUtils
unit which performs this calculation for you. So, if you can change your AJAX code to pass a Unix timestamp as-is to Delphi, you can let Delphi calculate a suitable TDateTime
.
Note, in this example, the resulting TDateTime
value is in UTC. After transmitting the value via AJAX to Delphi, if your Delphi code needs a TDateTime
in local time, that is a simple calculation to adjust the TDateTime
based on the local machine's timezone offset in minutes, which you can get using platform APIs, such as GetTimeZoneInformation()
on Windows, etc. Delphi has an IncMinute()
function in the DateUtils
unit that you can use for that adjustment.
UnixToDateTime()
andDateTimeToUnix()
functions take that into account.