Solved

JavaScript to delphi function

Posted on 2011-03-21
4
1,476 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:Ephraim Wangoya
Ephraim Wangoya 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Firemonkey Queue list 1 32
Broadcast a message using ICS 2 28
Use Mid in Html 6 23
Popup write two lines 3 22
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

730 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