Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JavaScript to delphi function

Posted on 2011-03-21
4
Medium Priority
?
1,589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

604 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