I have a webpage that, when you click start patrolling, starts counting the time since you clicked the button. Because I need to store this on the server (ASP.NET) and because I want to resume the count when they reload the page, I get two variables from the server. The first variable is the server time they clicked the start patrolling button. The second variable is the offset of the local time from the server time so we are 100% exact on the amount of time and it isn’t off because of the local time. The following code works but the hours are WAY off. Currently the local time is 19:53 and if this function runs it starts at “18:00:00” and then goes up “18:00:01”, “18:00:02” etc… If the local time was 20:53 it would be “19:00:00”. Why is this function doing this and how can I correct this?
patroltimeCurrentTimeoffset is the returned offset value (EG: -729)
patroltimeStart is the date and time the on the server that the user started patrolling (EG: April 10, 2012 20:01:13)
function UpdatePatrolTime() {
var patroltimeCurrentTime = new Date();
patroltimeCurrentTime.setTime(patroltimeCurrentTime.getTime() + patroltimeCurrentTimeoffset);
var patroltimeStarted = new Date(patroltimeStart);
var CompareDateTime = new Date((patroltimeCurrentTime.getTime()-patroltimeStarted.getTime()));
currentHours = CompareDateTime.getHours();
currentMinutes = CompareDateTime.getMinutes();
currentSeconds = CompareDateTime.getSeconds();
currentHours = (currentHours < 10 ? "0" : "") + currentHours;
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds
document.getElementById("patroltime").firstChild.nodeValue = currentTimeString;
}
And the <span> control:
<span id="patroltime">00:00:00</span>
I found a solution from “How to get the time elapsed from a json date?” but if anybody is able to figure out why my solution above doesn’t work (so I can award someone points for explaining for me and possibly others) I will award them the points. My solution was to change the following code:
to