2

I need to send via AJAX a date for a delphi server that accept the date in float format and set a TDateTime property.

Eg.

var
   date: TDateTime;
begin
   date := StrToFloat(Request.QueryFields.Values['date']);
end;

Delphi TDateTime start from 30/12/1989, i have tested with:

var
   date: TDateTime;
begin
   date := StrToFloat('0');
   ShowMessage( DateTimeToStr(date) ); // show: 30/12/1899
end;

JavaScript Date start from unix epoch, i have tested with:

console.log(new Date(0)); // Thu Jan 01 1970 01:00:00 GMT+0100

a simple conversion seem subtract the difference, but doesn't works, eg.:

// javascipt
var delphiTime = (new Date("07-24-2019") - new Date("12-30-1899")) / 1000;
console.log(delphiTime ); // 3773084400


// delphi
ShowMessage( DateTimeToStr(3773084400) ); // show 00/00/00

the strange fact, on delphi, Now is 43670.654378:

ShowMessage( FloatToStr(Now) ); // 43670.654378

In delphi 0 is 1899 and 43670 is 2019...

How works the date format in Delphi and 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

UPDATE:

In Delphi the float value = 1 would be 31.12.1899, 2 = 01.01.1900 and so on. Each unit seem a day.

function jsDateToDelphiDate(date){
  const seconds = (new Date(date).getTime() - new Date("12-30-1899").getTime()) / 1000;
  const days = seconds / 60 / 60 / 24;
  return days;
}

console.log(jsDateToDelphiDate(new Date("07-24-2019 16:00:00"))); // 43670.625

43670.625 on delphi is 23/07/2019 15:00.

Why i lose 1 hour?

3
  • It doesn't work because of the concept of leap years. Every 4 years there's an extra day in February. So you can't just simply do math on dates, not that easily anyway. Jul 24, 2019 at 13:51
  • @JerryDodge right! I had forgotten February :(
    – ar099968
    Jul 24, 2019 at 13:54
  • @JerryDodge The OP is losing 1 hour, not 1 day, so leap years is not a factor here. Since both Unix time and Delphi time are represented as simple offsets from given epochs, and neither one cares about how many days are in each month of a given year, simple math will work just fine to convert one to the other. Even Delphi's own UnixToDateTime() and DateTimeToUnix() functions take that into account. Jul 24, 2019 at 17:19

3 Answers 3

3

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.

0

In JS, valueOf Date is in milliseconds. If you want to convert it to days, simply divide it by 24*60*60*1000.

0

To convert the return value of new Date().valueOf() to Delphi TDateTime you can use this function:

function jsDateToDelphiDate(millis: int64): TDateTime;
begin
  var UnixSeconds: Integer := Round(millis / MSecsPerSec);
  Result := UnixToDateTime(UnixSeconds, False);
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.