Home > Delphi, JavaScript, Object Pascal, Smart Mobile Studio > JS date time under Delphi?

JS date time under Delphi?

I got an interesting question from a Smart Mobile Studio user that I feel should be adressed. Namely that of date-time conversion between Delphi and Smart Mobile Studio applications.

Our user has a Delphi back-end server written with Remobjects SDK, and part of the data exchange between the server and HTML5 client includes TDateTime fields. The TDateTime values work fine in Smart pascal, but generates the wrong date and time when used with a native JavaScript framework.

Different formats

To answer his question: that is to be expected! We have gone to great lengths to make Smart Mobile Studio support and handle date and time in Delphi compatible format. So when writing vanilla Smart Pascal code, the compiler and RTL makes sure the format is identical to Delphi.

108-37-date-format-with-javascript

The default date-time encoding used by JavaScript is very different from Delph and C++ builder. JavaScript actually uses the amount of milliseconds since 01.01.1970 at 00:00:00 o’clock.

So, to generate a JavaScript compatible date value, you can do this:

uses
  dateutils;

function TForm1.GetJSDateTime:Double;
begin
  result := MilliSecondsBetween(now,
  EncodeDateTime(1970,01,01,0,0,0,0));
end;

The same code compiles¬†under Smart Mobile Studio, except there the unit is named “system.dateutils”. But the result is the same.

To convert from JS format back to Delphi, have a look at the W3C.Date and System.Date units. You should find all the conversion and extraction functions you need (and then some).

 

  JDate = class external 'Date'
    constructor Create; overload;
    constructor Create(milliseconds : Integer); overload;
    constructor Create(dateString : String); overload;
    constructor Create(year, month, day : Integer); overload;
    constructor Create(year, month, day, hour, minute, second, msec : Integer); overload;

    class method now : Integer;
    class method parse(dateString : String) : Integer;
    class method UTC(year, month, day : Integer) : Integer; overload;
    class method UTC(year, month, day, hour, minute, second, msec : Integer) : Integer; overload;

    function getDate : Integer;
    function getDay : Integer;
    function getFullYear : Integer;
    function getHours : Integer;
    function getMilliseconds : Integer;
    function getMinutes : Integer;
    function getMonth : Integer;
    function getSeconds : Integer;

    function getTime : Integer;
    function getTimezoneOffset : Integer;

    function getUTCDate : Integer;
    function getUTCDay : Integer;
    function getUTCFullYear : Integer;
    function getUTCHours : Integer;
    function getUTCMilliseconds : Integer;
    function getUTCMinutes : Integer;
    function getUTCMonth : Integer;
    function getUTCSeconds : Integer;

    procedure setDate(v : Integer);
    procedure setFullYear(v : Integer);
    procedure setHours(v : Integer);
    procedure setMilliseconds(v : Integer);
    procedure setMinutes(v : Integer);
    procedure setMonth(v : Integer);
    procedure setSeconds(v : Integer);

    procedure setTime(v : Float);

    procedure setUTCDate(v : Integer);
    procedure setUTCFullYear(v : Integer);
    procedure setUTCHours(v : Integer);
    procedure setUTCMilliseconds(v : Integer);
    procedure setUTCMinutes(v : Integer);
    procedure setUTCMonth(v : Integer);
    procedure setUTCSeconds(v : Integer);

    function toDateString : String;
    function toISOString : String;
    function toLocaleDateString : String;
    function toLocaleString : String;
    function toString : String;
    function toTimeString : String;
    function toUTCString : String;
  end;
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: