0
function ageCount() {
    var date1 = new Date();
    var dob = document.getElementById("dob").value;
    var date2 = new Date(dob);
    var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
    if (pattern.test(dob)) {
        var y1 = date1.getFullYear();         
        var y2 = date2.getFullYear();
        var age = y1 - y2;
        document.getElementById("age").value = age;
        document.getElementById("age").focus ();
        return true;
    }else {
        alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
        return false;
    }
}

I am having time computing the exact age of person. for example the date today is July 20, 2018 (7/20/2018) and my birthday is December 17,1988 (12/17/1988) it gives me result that I m 30 years old but presently I am 29 and because my birthday is December 17 that is the time where I will be 30 years old. pls. help me figure out this problem and show me what is wrong with my code.

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17

1 Answers1

0

Convert date into Unix Time and then take Difference and again Change the Unix time difference to Date

<html>

<head>
  <script type="text/javascript">
    function checkDate() {
      var date1;
      var date2;
      date1 = document.getElementById('date1').value;
      date2 = document.getElementById('date2').value;
      var btn = document.createElement("p");

      if (date1 != '' && date2 != '') {
        var datx = getAge(new Date(date1), new Date(date2));
        var t = document.createTextNode(datx);
        btn.appendChild(t);
        document.body.appendChild(btn);
      }
    }

    function getAge(date_1, date_2) {
      var date2_UTC = new Date(Date.UTC(date_2.getUTCFullYear(), date_2.getUTCMonth(), date_2.getUTCDate()));
      var date1_UTC = new Date(Date.UTC(date_1.getUTCFullYear(), date_1.getUTCMonth(), date_1.getUTCDate()));
      var yAppendix, mAppendix, dAppendix;
      var days = date2_UTC.getDate() - date1_UTC.getDate();
      if (days < 0) {

        date2_UTC.setMonth(date2_UTC.getMonth() - 1);
        days += DaysInMonth(date2_UTC);
      }
      var months = date2_UTC.getMonth() - date1_UTC.getMonth();
      if (months < 0) {
        date2_UTC.setFullYear(date2_UTC.getFullYear() - 1);
        months += 12;
      }
      var years = date2_UTC.getFullYear() - date1_UTC.getFullYear();
      if (years > 1) yAppendix = " years";
      else yAppendix = " year";
      if (months > 1) mAppendix = " months";
      else mAppendix = " month";
      if (days > 1) dAppendix = " days";
      else dAppendix = " day";
      return years + yAppendix + ", " + months + mAppendix + ", and " + days + dAppendix + " old.";
    }

    function DaysInMonth(date2_UTC) {
      var monthStart = new Date(date2_UTC.getFullYear(), date2_UTC.getMonth(), 1);
      var monthEnd = new Date(date2_UTC.getFullYear(), date2_UTC.getMonth() + 1, 1);
      var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24);
      return monthLength;
    }
  </script>
</head>

<body>
  <input type="date" name="date" id="date1">
  <input type="date" name="date2" id="date2">
  <button onclick="checkDate()">Check Date</button>
</body>

</html>
Ahtisham Ashraf
  • 684
  • 1
  • 6
  • 12