Solved

JavaScript to delphi function

Posted on 2011-03-21
4
1,450 Views
Last Modified: 2012-05-11
Hi Experts,

I need to convert a javascript function to Delphi

var mytime = new Date().getTime();
  alert('Time in milliseconds : '+mytime);

Date.getTime() is a prebuilt JavaScript method that returns the time elapsed from January 1st, 1970 to the current Date instance, in milliseconds

How to do this in delphi ?

Thanks
0
Comment
Question by:alpires
  • 3
4 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35184107
Check this please:
procedure TForm1.Button2Click(Sender: TObject);
var
  DT:TDateTime;
begin
  ShortDateFormat := 'dd/mm/yyyy';
  DT := StrToDateTime('01/01/1970 03:00:00 AM'); // I was trying out until I arrived to this, but I am not sure though if it is the correct time
  showmessage(floattostr(    (now-DT)*24*60*60)    );
end;

Open in new window

0
 
LVL 24

Accepted Solution

by:
jimyX earned 250 total points
ID: 35184240
To have it in Milliseconds:
procedure TForm1.Button2Click(Sender: TObject);
var
  DT:TDateTime;
  str:String;
begin
  ShortDateFormat:='dd/mm/yyyy';
  DT := StrToDateTime('01/01/1970 03:00:00 AM');
  Str := floattostr((now-DT)*24*60*60*1000);
  showmessage('Time in milliseconds : '+ Str);
end;

Open in new window

0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 250 total points
ID: 35184339
@jimyx
you work too hard

here, uses functions in DateUtils

uses
  DateUtils;


var
  mlsecs: Int64;
begin
  mlsecs := MilliSecondsBetween(NOW, StrToDateTime('01/01/1970 03:00:00 AM'));
  ShowMessage(IntToStr(mlsecs));
end;
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35184488
@ ewangoya
We all do ;-) don't you?

And BTW, what I did is exactly what Delphi does behind the scene:
// From DateUtils and SysUtils.
{ Units of time }
  HoursPerDay   = 24;
  MinsPerHour   = 60;
  SecsPerMin    = 60;
  MSecsPerSec   = 1000;
  MinsPerDay    = HoursPerDay * MinsPerHour;
  SecsPerDay    = MinsPerDay * SecsPerMin;
  MSecsPerDay   = SecsPerDay * MSecsPerSec;

function SpanOfNowAndThen(const ANow, AThen: TDateTime): TDateTime;
begin
  if ANow < AThen then
    Result := AThen - ANow
  else
    Result := ANow - AThen;
end;

function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;
begin
  Result := Trunc(MilliSecondSpan(ANow, AThen));
end;

function MilliSecondSpan(const ANow, AThen: TDateTime): Double;
begin
  Result := MSecsPerDay * SpanOfNowAndThen(ANow, AThen);
end;

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question