Solved

JavaScript to delphi function

Posted on 2011-03-21
4
1,467 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…

829 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