title: how to display years, months, weeks and days since a date
body: <p>Hope you are all well,</p>
  
  <p>I've been struggling to find a way to display the amount of time that has passed since a specific date in years, months, weeks and days. The closest i have found, is this below but i can quite work out how to get it to display the years and months.</p>
  
  <p>Any tips would be greatly appreciated.</p>
  
  <p>Thanks</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>window.onload = function() {
    doTime('jan,01,2017,00:00:01');
  }
  
  function doTime(then) {
  
    now = new Date();
    then = new Date(then);
  
    difference = (now - then);
  
    days = Math.floor(difference / (60 * 60 * 1000 * 24) * 1);
    hours = Math.floor((difference % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
    mins = Math.floor(((difference % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
    secs = Math.floor((((difference % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
  
    document.getElementById('timer').firstChild.nodeValue =
  
      +days + ' days ' + hours + ' hours ' + mins + ' minutes ' + secs + ' seconds';
    clearTimeout(doTime.to);
    doTime.to = setTimeout(function() {
      doTime(then);
    }, 1000);
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="timer"&gt;&amp;nbsp;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>—
  thanks for the suggestion of the previous post, sadly i have tried that and i can only get it to work the difference between two actual dates, i cant get it to automatically make the end date now so it counts up automatically as time goes on.</p>
  
  <p>—
  i have done some more fiddling and managed to get to this, being new to js, would you guys say this is pretty close? thanks</p>
  
  <pre><code>var startDateTime = new Date(2012,5,24,09,43,0,0); // YYYY (M-1) D H m s 
  (start time and date from DB)
  var startStamp = startDateTime.getTime();
  
  var newDate = new Date();
  var newStamp = newDate.getTime();
  
  var timer;
  
  function updateClock() {
      newDate = new Date();
      newStamp = newDate.getTime();
      var diff = Math.round((newStamp-startStamp)/1000)
  
      var years = Math.floor(diff/(12*4.3479*7*24*60*60));
       diff = diff-(years*12*4.3479*7*24*60*60)
  
  
      var months = Math.floor(diff/(4.3479*7*24*60*60));
      diff = diff-(months*4.3479*7*24*60*60)
  
      var weeks = Math.floor(diff/(7*24*60*60));
      diff = diff-(weeks*7*24*60*60)
  
      var days = Math.floor(diff/(24*60*60));
      diff = diff-(days*24*60*60);
      var hours = Math.floor(diff/(60*60));
      diff = diff-(hours*60*60);
      var mins = Math.floor(diff/(60));
      diff = diff-(mins*60);
      var secs = diff;
  
      document.getElementById("time-elapsed").innerHTML = years+" years, 
  "+months+" months, " +weeks+" weeks, " +days+" days, "+hours+" hours and 
  "+mins+" minutes,";
  }
  
  setInterval(updateClock, 1000);
  
  
  
  &lt;div id="time-elapsed"&gt;&lt;/div&gt;
  </code></pre>