title: JavaScript to calculate Age based on date
body: <p>Currently am using JavaScript code to calculate age based on date of birth given by user, its working good but the age showing is wrong.</p>
  
  <p>mycode :</p>
  
  <pre><code>function getAge(dateString) {
    var now = new Date();
    var today = new Date(now.getYear(),now.getMonth(),now.getDate());
  
    var yearNow = now.getYear();
    var monthNow = now.getMonth();
    var dateNow = now.getDate();
  
    var dob = new Date(dateString.substring(6,10),
                       dateString.substring(0,2)-1,                   
                       dateString.substring(3,5)                  
                       );
  
    var yearDob = dob.getYear();
    var monthDob = dob.getMonth();
    var dateDob = dob.getDate();
    var age = {};
    var ageString = "";
    var yearString = "";
    var monthString = "";
    var dayString = "";
  
  
    yearAge = yearNow - yearDob;
  
    if (monthNow &gt;= monthDob)
      var monthAge = monthNow - monthDob;
    else {
      yearAge--;
      var monthAge = 12 + monthNow -monthDob;
    }
  
    if (dateNow &gt;= dateDob)
      var dateAge = dateNow - dateDob;
    else {
      monthAge--;
      var dateAge = 31 + dateNow - dateDob;
  
      if (monthAge &lt; 0) {
        monthAge = 11;
        yearAge--;
      }
    }
  
    age = {
        years: yearAge,
        months: monthAge,
        days: dateAge
        };
  
    if ( age.years &gt; 1 ) yearString = " years";
    else yearString = " year";
    if ( age.months&gt; 1 ) monthString = " months";
    else monthString = " month";
    if ( age.days &gt; 1 ) dayString = " days";
    else dayString = " day";
  
  
    if ( (age.years &gt; 0) &amp;&amp; (age.months &gt; 0) &amp;&amp; (age.days &gt; 0) )
      ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old.";
    else if ( (age.years == 0) &amp;&amp; (age.months == 0) &amp;&amp; (age.days &gt; 0) )
      ageString = "Only " + age.days + dayString + " old!";
    else if ( (age.years &gt; 0) &amp;&amp; (age.months == 0) &amp;&amp; (age.days == 0) )
      ageString = age.years + yearString + " old. Happy Birthday!!";
    else if ( (age.years &gt; 0) &amp;&amp; (age.months &gt; 0) &amp;&amp; (age.days == 0) )
      ageString = age.years + yearString + " and " + age.months + monthString + " old.";
    else if ( (age.years == 0) &amp;&amp; (age.months &gt; 0) &amp;&amp; (age.days &gt; 0) )
      ageString = age.months + monthString + " and " + age.days + dayString + " old.";
    else if ( (age.years &gt; 0) &amp;&amp; (age.months == 0) &amp;&amp; (age.days &gt; 0) )
      ageString = age.years + yearString + " and " + age.days + dayString + " old.";
    else if ( (age.years == 0) &amp;&amp; (age.months &gt; 0) &amp;&amp; (age.days == 0) )
      ageString = age.months + monthString + " old.";
    else ageString = "Oops! Could not calculate age!";
  
    return ageString;
  }
  $(".date").on("change", function(){
  var dob = $('.date').val();
   var age = getAge(dob); 
   $('.age').val(age);
  });
  </code></pre>
  
  <p>Here is link of jsfiddle <a href="http://jsfiddle.net/ursdineshr/bzavye3L/1/" rel="nofollow">Click Here</a></p>
  
  <p>If i give a date 21/03/1993 am getting age as 20, but the correct age is 23</p>