Solved

JavaScript to delphi function

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now