Click to See Complete Forum and Search --> : Using Date Object


tomyknoker
06-20-2003, 07:33 PM
Hi All,
I have three buttons on a page called, "button 1", "button 2" etc. Using the DATE OBJECT I want to track a users time on the page. Also store information on what time (including seconds) it was when they pressed each of the three buttons on the page.

Then when the user leaves the page to go somewhere else create a new pop up window and print to it the time they entered the previous page, time they left the total time in seconds they spent on the page. Also what time each of the buttons where pressed.

I realise this is quite easy but I am just learning Javascript and picking things up slowly, but I want to do this the simplest way possible without getting to complicated as I am only just beginning my JavaScriopt days...

Thanks everyone for help in advance,
Tom

AdamBrill
06-20-2003, 08:21 PM
Try using the attached files... Let me know if you need it modified and can't figure it out... ;)

tomyknoker
06-20-2003, 09:33 PM
hey Adam,
thanks the attachment was such a help...but there was just a few things that I needed to work out which I couldn't work out. When the three buttons are pressed I want it to display the time like this 12:25:02 but I couldn't figure it out and also how do I change the time from milliseconds to seconds.
thanks again,
tom

AdamBrill
06-20-2003, 10:24 PM
Ok... Try this one. I think that should be closer. ;)

tomyknoker
06-21-2003, 03:41 AM
hey adam,
not sure seems to still be a bit funny when I click to the next page it displays this:

All times in Seconds:
Other page loaded: 1056169411 (*I WANT THESE PARTS TO DISPLAY LIKE 14:23:45)
Button1 pushed: 1056169427 (*)
Button2 pushed: 1056169426 (*)
Button3 pushed: 1056169427 (*)
Other page unloaded: 1056169427 (*)
Time spent on page: 16
Times loaded and unloaded:
Time page loaded: 14:23:31 (ALSO HERE THE PAGE LOADED AT 6.30 IN THE EVENING BUT IT SEEMED TO SAY IT LOADED AT 3.23pm)
Time page unloaded: 14:23:47 (ALSO HERE THE PAGE LOADED AT 6.30 IN THE EVENING BUT IT SEEMED TO SAY IT LOADED AT 3.23pm)


Thanks Agian,
Tom

tomyknoker
06-21-2003, 08:47 PM
can anyone help me with the above am a bit stuck!

tomyknoker
06-21-2003, 09:19 PM
Hi Dave,
but I am not sure how to make this work for my page as the out put doesn't seem to work right here is the file,
tom

tomyknoker
06-23-2003, 07:37 AM
Hi Everyone,
I have three buttons on my page that when pressed are tracked and when the user goes to another page are told how long they spent and what time each button was pressed. It all works but when the buttons are pressed it displays a rather long number, xcheck out my code below:

PAGE 1
<html>
<head>
<title>Untitled</title>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!-- Begin
thetime = new Date();
page_load = thetime.getTime();
function button1(){
thetime = new Date();
button_1 = thetime.getTime();
}
function button2(){
thetime = new Date();
button_2 = thetime.getTime();
}
function button3(){
thetime = new Date();
button_3 = thetime.getTime();
}
function goto_nextpage(){
thetime = new Date();
document.location="nextpage.htm?page_load="+page_load+"&button_1="+button_1+"&button_2="+button_2+"&button_3="+button_3+"&page_leave="+thetime.getTime();
}

//-->
</SCRIPT>
</head>

<body>

<input type=button onclick="button1()" value="button1"><br>
<input type=button onclick="button2()" value="button2"><br>
<input type=button onclick="button3()" value="button3"><br>
<a href="nextpage.htm" onclick="goto_nextpage(); return false;">Next Page</a>
</body>
</html>

PAGE 2
<html>
<head>
<title>Untitled</title>
<script language=javascript type="text/javascript">
variables = String(document.location).split("?")[1].split("&");
for(x=0; x<variables.length; x++){
eval("var "+variables[x].split('=')[0]+"="+variables[x].split('=')[1]);
}
</script>
</head>

<body>
All times in Seconds:<br>
<script language=javascript type="text/javascript">
document.write("Other page loaded: "+Math.round(page_load/1000)+"<br>");
document.write("Button1 pushed: "+Math.round(button_1/1000)+"<br>");
document.write("Button2 pushed: "+Math.round(button_2/1000)+"<br>");
document.write("Button3 pushed: "+Math.round(button_3/1000)+"<br>");
document.write("Other page unloaded: "+Math.round(page_leave/1000)+"<br>");
document.write("Time spent on page: "+Math.round((page_leave-page_load)/1000)+"<br>");
document.write("<br><br>");
document.write("Times loaded and unloaded:<br>");
page_load_date = new Date(page_load);
document.write("Time page loaded: "+((String(page_load_date.getHours()).length<2)?"0":"")+page_load_date.getHours()+":"+((String(page_load_date.getMinutes()).length<2)?"0":"")+page_load_date.getMinutes()+":"+((String(page_load_date.getSeconds()).length<2)?"0":"")+page_load_date.getSeconds()+"<br>");
page_leave_date = new Date(page_leave);
document.write("Time page unloaded: "+((String(page_leave_date.getHours()).length<2)?"0":"")+page_leave_date.getHours()+":"+((String(page_leave_date.getMinutes()).length<2)?"0":"")+page_leave_date.getMinutes()+":"+((String(page_leave_date.getSeconds()).length<2)?"0":"")+page_leave_date.getSeconds()+"<br>");

</script>

</body>
</html>

pyro
06-23-2003, 07:55 AM
To turn a date that was returned in milliseconds to a more readable form, try something like this:

<script type="text/javascript">
newdate = new Date(time); //time is the time in milliseconds

month = newdate.getMonth();
month++;
day = newdate.getDate();
year = newdate.getYear();

hours = newdate.getHours();
minutes = newdate.getMinutes();
seconds = newdate.getSeconds();

document.write(month+"-"+day+"-"+year+" at "+hours+":"+minutes+":"+seconds);
</script>

pyro
06-23-2003, 08:09 AM
Actually, something like this may be better... It will return it like this: MM-DD-YYYY at H-MM-SS

<script type="text/javascript">
newdate = new Date(time); //time is the time in milliseconds


month = newdate.getMonth();
month++;
month = month < 10 ? "0"+month : month;
day = newdate.getDate();
day = day < 10 ? "0"+day : day;
year = newdate.getYear();

hours = newdate.getHours();
minutes = newdate.getMinutes();
minutes = minutes < 10 ? "0"+minutes : minutes;
seconds = newdate.getSeconds();
seconds = seconds < 10 ? "0"+seconds : seconds;

document.write(month+"-"+day+"-"+year+" at "+hours+":"+minutes+":"+seconds);
</script>

tomyknoker
06-23-2003, 08:16 AM
Hi thanks for that,
sorry if I have been unclear have only been using javascript for three weeks and still trying to get my head around it. My code is above and on the second page say for instance the code from my buttons looks like this:

document.write("Button1 pushed: "+Math.round(button_1/1000)+"<br>");
document.write("Button2 pushed: "+Math.round(button_2/1000)+"<br>");
document.write("Button3 pushed: "+Math.round(button_3/1000)+"<br>");

this obviously outputs into milliseconds but I just can't seem to work out where to place the hrs:mins:secs
thanks again in advance,
tom

pyro
06-23-2003, 08:41 AM
I'm assumbing that your button_1, button_2, and button_3 are the time in milliseconds... So, just input that in to my code instead of time in the first line... You will have to run through the code for each of the three buttons...