Convert javascript time calculation to asp?

Hi, I am tracking user time and have the following javascript code that works fine,except that I want to do same thing in ASP so that I can put it in global.asa session_onend subroutine.  Anyone know the equivalent ASP for the below javascriptcode block?

<script language="JavaScript" type="text/JavaScript">
var startDate = new Date();
var startTime = startDate.getTime();

  var endDate = new Date();
  var endTime = endDate.getTime();
  var deltaDate = new Date(endTime - startTime);
  var deltaTime = deltaDate.getTime();
  var deltaSec = (deltaTime/1000);
  //startTime = endTime;

  var t=deltaSec;
  var x=3600;
  var y=60;
  var h=Math.round(t/x - t%x/x)+'';
  var m=Math.round((t-h*x)/y-(t-h*x)%y/y)+'';
  var s=Math.round(t-h*x-m*y)+'';
  if (h.toString().length == 1) h='0'+h;
  if (m.toString().length == 1) m='0'+m;
  if (s.toString().length == 1) s='0'+s;
  var data = h+":"+m+":"+s;
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I am not sure how you are going to call these.

you can get the Date/Time with the Now() method.

dim datStart, datEnd

datStart = Now()

To calculate the difference, try dateDiff()

numMinutes =DateDiff("d",datStart, datEnd)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZvonkoSystems architectCommented:
The equivalent code is this:
  data = "00:00:00"

The upper JavaScript cannot produce a value for deltaSec greater then zero seconds. Because calling two times new Date() is running in miliseond speed.
So you surely miss some parameter for the new Date(), either for startDate or for the endDate var.
The one of them can stay empty, but noth both.
cbecker696969Author Commented:
The above JS gets a startime (on login page) and endtime (on logout page) in milliseconds and calculates how much time elapsed. Then converts the value to hh:mm:ss format.  

I need to calculate in ASP the  total time of session (endtime-starttime) in hh:mm:ss format.   Does that make sense?
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

It does, but how will you actually know when the user logs off? If you have a log off button that calls a session.abandon, then you are set. However, if the user navigates away or closes the browser, then you won't have a Session_OnEnd until the session timeout expires (typically 15-20 minutes). Moreover, the Session_OnEnd is notoriously unreliable (it very often does not fire at the end of the session).

ZvonkoSystems architectCommented:
The upper JavaScript calculates one page display time in browser (if the part after the comment //calculateTime called in a function at page submit or unload).
cbecker696969Author Commented:
I have implemented a logout system that so far does not employ the session_onend at all. It  It works fine when user clicks logoff.  Also works when user closes browser (see code below):

<SCRIPT FOR=window EVENT=onunload>
      if(!(window.screenLeft <10004))  'this is close browser
            var hwnd ="/exit.asp"); 'calculates time and adds to database
      else    'it is just refresh so do nothing

 The only other way a user could exit then is navigate away.  If I could detect this and redirect to exit.asp and then let browser continue on its way out of site that would work. But I can'tfigure out how to do that so I am thus trying to trigger session_onend to do necessary time calculation and DB record update for this scenario of navigating away.

20 min delay is acceptable.  But if as you say session_onend is unreliable then that will not work as I need to ensure that each users total time is ALWAYS calculated and updated as they are paying customers based on their time usuage.

Do you know how I could avoid the session_onend altogether and still deal with navigating away scenario?
cbecker696969Author Commented:
Hi Zvonko, yes that is right.The upper JS captures starttime and the lower is called on logout.
Not that I know of. I have seen people use a frame set and then use the onUnLoad event of the parent frame to call a session.abandon().

ZvonkoSystems architectCommented:
For paying customers is a method of pooling a script the best method.
Like this:

function poolTimer(){
  var newImg = new Image();
  newImg.src = "yourpool.asp?time="+(new Date()).getTime();
  setTimeout(poolTimer, 5000);


As long as the page is displayed you get every five seconds a pool request from that page. A separate scanner routine on server side can filter that pools and calculate the session duration.

The time parameter is a random string to keep image loading instead of caching.

cbecker696969Author Commented:
Zvonko, I am not sure i am following you.  I need to calculate duration between start and end timeof user session in hh:mm:ss format.  Is your script above for that purpose?
ZvonkoSystems architectCommented:
No, it is not.
The upper function poolTimer() will generate every five seconds an ASP call to your server.
In that called asp (in my example is the file name yourpool.asp) you can handle some heartbeat function and trigger some still-alive counter.
cbecker696969Author Commented:
Thanks but I can't see how that is useful for my current need.  
ZvonkoSystems architectCommented:
Sorry, my bad. I did not want to say pool, I wanted to say: poll
Now you are polling your server by the upper JavaScript function and can write every poll timestamp. If the previous poll was in time interval, then you have a session and can write at every poll call the session duration. After no polls occure the session duration time will not be updated and you have the final duration of the session after the last poll.
cbecker696969Author Commented:
I am starting to grok what your are suggesting. Unfortunately I have to sign off till tomorrow.  Hopefully we can pick it up again then.  Cheers
ZvonkoSystems architectCommented:
See you.
The question was answered as asked here:


many ot the other posts provide useful information regarding this common issue of tracking session time, so I don't think a delete is appropriate.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.