title: Find elapsed time in javascript
body: <p>I'm new to JavaScript and I'm trying to write a code which calculates the time elapsed from the time a user logged in to the current time. </p>
  
  <p>Here is my code:-</p>
  
  <pre><code>function markPresent() {
      window.markDate = new Date();
      $(document).ready(function() {
          $("div.absent").toggleClass("present");
      });
      updateClock();
  }
  
  function updateClock() {    
      var markMinutes = markDate.getMinutes();
      var markSeconds = markDate.getSeconds();
  
      var currDate = new Date();
      var currMinutes = currDate.getMinutes();
      var currSeconds = currDate.getSeconds();
      var minutes = currMinutes - markMinutes;
      if(minutes &lt; 0) { minutes += 60; }
      var seconds = currSeconds - markSeconds;
      if(seconds &lt; 0) { seconds += 60; }
  
      if(minutes &lt; 10) { minutes = "0" + minutes; }
      if(seconds &lt; 10) { seconds = "0" + seconds; }
  
      var hours = 0;
      if(minutes == 59 &amp;&amp; seconds == 59) { hours++; }
      if(hours &lt; 10) { hours = "0" + hours; }
  
      var timeElapsed = hours+':'+minutes+':'+seconds;
      document.getElementById("timer").innerHTML = timeElapsed;
      setTimeout(function() {updateClock()}, 1000);
  }
  </code></pre>
  
  <p>The output is correct upto 00:59:59 but after that that O/P is:</p>
  
  <p>00:59:59
  01:59:59
  01:59:00
  01:59:01
  .
  .
  .
  .
  01:59:59
  01:00:00</p>
  
  <p>How can I solve this and is there a more efficient way I can do this?
  Thank you.</p>