title: Calculating year, month, days between dates in google apps script
body: <p>I have calculated using the below function and it gives the o/p in the format of "X YEARS, Y MONTHS, Z DAYS" and for some dates its giving some wrong o/p. I think I did some calculation missing in the formulas.</p>
  
  <p>The function is,</p>
  
  <pre><code>/**
   * @param {Date} startdate
   * @param {Date} enddate
   * @return {String}
   */
  function leasePeriodCalc(startDate,endDate)
  {
    var sdate=startDate;
    var edate=endDate;
    edate.setDate( edate.getDate()+1);
    edate=new Date(edate);
    if(sdate.valueOf()&gt;edate.valueOf()){
      return('0');
    }
    else{
      var years=((((edate.getDate()-sdate.getDate())&lt;0 ? -1:0)+((edate.getMonth()+1)-(sdate.getMonth()+1)))&lt; 0 ? -1 : 0)+(edate.getFullYear()-sdate.getFullYear());
      var months=((((edate.getDate()-sdate.getDate())&lt;0 ? -1:0)+((edate.getMonth()+1)-(sdate.getMonth()+1)))&lt; 0 ?12:0)+((edate.getDate()-sdate.getDate())&lt;0 ? -1:0)+((edate.getMonth()+1)-(sdate.getMonth()+1));
      if((edate.getMonth()-1)!=1.0)
      {
        var days=((edate.getDate()-sdate.getDate())&lt; 0 ?new Date(edate.getFullYear(), edate.getMonth(),0).getDate():0)+(edate.getDate()-sdate.getDate());
      }
      else
      {
        var days=((edate.getDate()-sdate.getDate())&lt; 0 ?new Date(edate.getFullYear(), edate.getMonth()+1,0).getDate():0)+(edate.getDate()-sdate.getDate());
      }
      var day;
      var month;
      var year;
      if(years&gt;1)year= years+ 'Years';
      else year=years+'Year';
      if(months&gt;1) month= months+ 'Months';
      else month=months+'Month';
      if(days&gt;1) day= days+ 'Days';
      else day=days+'Day';
      if(years==0&amp;&amp;months!=0&amp;&amp;days!=0) return(month+', '+day);
      else if(years!=0&amp;&amp;months==0&amp;&amp;days!=0) return(year+', '+day);
      else if(years!=0&amp;&amp;months!=0&amp;&amp;days==0) return(year+', '+month);
      else if(years==0&amp;&amp;months==0&amp;&amp;days!=0) return(day);
      else if(years==0&amp;&amp;months!=0&amp;&amp;days==0) return(month);
      else if(years!=0&amp;&amp;months==0&amp;&amp;days==0) return(year);
      else if(years==0&amp;&amp;months==0&amp;&amp;days==0) return(day);
      else if(years!=0&amp;&amp;months!=0&amp;&amp;days!=0) return(year+', '+month+', '+day);
    }
  }
  </code></pre>
  
  <p>if you gives the i/p as below it returning the false o/p:</p>
  
  <p>28th feb 2013 - 28th feb 2014   </p>
  
  <p>Expected o/p : 1 YEAR , 1 DAY</p>
  
  <p>Given o/p    : 1 YEAR , 4 DAYS</p>
  
  <p>But if I select 28th feb 2013 - 27th feb 2014 means, It gave the correct o/p:</p>
  
  <p>Expected o/p : 1 YEAR</p>
  
  <p>Given o/p    : 1 YEAR</p>
  
  <p>Please advice to correct my fault if I did anything.</p>
  
  <p>And also I have to tell that I'm not setting the rules n all. In general a month is calculating as per the days lying on the month. </p>
  
  <p>For example, If we get a loan from a bank we ll pay the interest per month only even that month may have 30 days or 29 days or 28 days or 31 days. </p>
  
  <p>And also if we take a room for monthly rental means, We ll pay the rent per month only rite? even it can be from 20th March - 19th April. Even it contains 31 days it is said to be one month only. Please help me to conclude this.</p>
  
  <p>Tnx,
  CL.</p>