title: Calculating timespan in years, months and days in JavaScript
body: <p>I want to calculate the timespan between two dates (note: input format is dd.MM.yyyy, see code below). Special thing is, that don't want to use 30 days for every  month and 360 days for a year. I rather want the difference in "human format" (don't know how to call it).</p>
  
  <p>Let's say, I want to calculate difference (including the last day) from October 1 (2014) until March 17, 2015. From October to February this would make 5 months. And then the rest would be 18 days (from day 1 to 17, including 17th day). So the result should be :</p>
  
  <blockquote>
    <p>0 years, 5 months, 18 days</p>
  </blockquote>
  
  <p>This sort of calculation of course ignores that some months have 31, 30, 28 or 29 days (except for the day calculation, when dates are in such a form: start date: October 17th, 2014; end date: Januar 12th, 2015).</p>
  
  <p>Unfortunately I didn't found any JS lib that already implements this sort of calculation. Moments.js, doesn't seem to have any methods for this.</p>
  
  <p>So I started creating my own code but I'm still always off some days (2-3 days) from the expected result. And to be honest, if I look at my code I have the feeling that it sucks and there must be a more intelligent and elegant way to calculate this (in my example I'm outputting it to a span):</p>
  
  <pre><code>function getTimeDifferenceContract(startDateStr, endDateStr) {
      var returnObject = { "years": null, "months": null, "days": null };
      var startDateArray = startDateStr.split('.');
      var endDateArray = endDateStr.split('.');
      var startMonthIx = startDateArray[1]-1;
      var endMonthIx = endDateArray[1]-1;
      var startDate = new Date(startDateArray[2], startMonthIx, startDateArray[0]);
      var endDate = new Date(endDateArray[2], endMonthIx, endDateArray[0]);
  
      var endDateFixed = new Date(endDate.getTime()+(1*24*60*60*1000));
  
      if (endDate &gt; startDate) {
          var years = 0;
          var months = 0;
          var days = 0;
          var dateDiff = endDateFixed.getTime() - startDate.getTime();
  
          var sD = startDate.getDate();
          var sM = startDate.getMonth()+1;
          var sY = startDate.getFullYear();
  
          var eD = endDateFixed.getDate();
          var eM = endDateFixed.getMonth()+1;
          var eY = endDateFixed.getFullYear();
  
          if (sY == eY &amp;&amp; sM == eM) {         
              days = Math.floor(dateDiff / (1000 * 60 * 60 * 24));
          }
          else if (sY == eY) {
              if (sD &gt; eD) {
                  months = eM - sM - 1;
                  var startMonthRestDays = getMonthdays(sM, sY) - sD;
                  days = startMonthRestDays + eD;
              }
              else {
                  months = eM - sM;
                  days = eD - sD;
              }
          }
          else {
              years = eY - sY - 1;
              var monthForYears = 0;
              if (years &gt; 0) {
                  monthForYears = (years - 1) * 12;
              } else {
                  monthForYears = years * 12;
              }
              months = (12 - sM) + (eM - 1) + (monthForYears)
              var startMonthRestDays = getMonthdays(sM, sY) - sD;
              days = startMonthRestDays + eD - 0;
          }
  
          var lastMonth = eM - 1;
          var yearForEndMonthDays = eY;
          if (lastMonth &lt; 1) {
              lastMonth = 12;
              yearForEndMonthDays = eY - 1;
          }       
          var endMonthDays = getMonthdays(lastMonth, yearForEndMonthDays);
          if (days &gt;= endMonthDays) {          
              months = months + 1;
              days = days - endMonthDays - 1;
          }
          if (months &gt;= 12) {
              years = years + 1;
              months = months - 12;
          }
  
          returnObject.years = years;
          returnObject.months = months;
          returnObject.days = days;
      }
      return returnObject;
  }
  
  function main() {   
      var difference = getTimeDifferenceContract("30.09.2014", "01.10.2015");
      var years = difference.years;
      var months = difference.months;
      var days = difference.days;
  
      jQuery('#myText').text(years + " years, " + months + " months, " + days + " days");
  }
  
  main();
  </code></pre>
  
  <p>Fiddle: <a href="http://jsfiddle.net/4ddL27gx/2/" rel="nofollow">http://jsfiddle.net/4ddL27gx/2/</a></p>
  
  <p>Any ideas to solve this?</p>