0

I am trying to calculate age using Javascript. The choose their date of birth from an HTML date input type and his/her age should be displayed. How can Javascript use the HTML Date input type data and calculate age?

Below is the HTML

<html>
<head>
<title> Sample Date of Birth Registration</title>

<script type="text/javascript" src="formiteration6.js"></script>
</head>
<body>
<h1>Birth Registration</h1>
<hr />
<form id ="inputFrom">
 <label for="size_1">D.O.B:</label><input type="date" name="size" id="birthDate" value="dd/mm/yy"  />
 <input type='button' onclick='regBirth()' value='Add new person' />
 </form>
 <hr />
<table id="details">
  <tr>
  <th>Date of Birth</th>
  <th>Age</th>
  </tr>

</table>
<h4>Statistics</h1>
<hr />
<h5><b>Total Count:</b></h5>
<p id="count"></p>

</body>
</html>

And Javascript is here

var allPeople = [];

function regBirth() {
    'use strict';
    var myArray = {};

    var actualDob = myArray.actualBirthDate;
    actualDob = document.getElementById('birthDate').value

    allPeople.push(myArray);

    var inputForm = document.getElementById("inputFrom").reset();
    var tabularForm = document.createDocumentFragment();
    var tablerow = document.createElement('tr'); 

    var dob = document.createElement('td');
    dob.innerHTML = actualDob;
    tablerow.appendChild(dob);

    tabularForm.appendChild(tablerow); 
    document.getElementById("details").appendChild(tabularForm);

    var totalPeople = allPeople.length;

    document.getElementById("count").innerHTML=totalPeople;
}
IslandKing
  • 27
  • 2
  • 12
  • See https://stackoverflow.com/a/7091965/1722529 — first, get difference between current year and the year of birth. Then, if current month number is less than birth month number, subtract 1 from year difference and use the result as age. – Matvey Andreyev Apr 05 '18 at 12:07

2 Answers2

0

Get Today's Date using new Date()

Get Date of Birth using new Date(datestring)

Get Year from both Dates using getFullYear()

Now find the Difference between two Years.

Fiddle Demo

Vinod
  • 4,672
  • 4
  • 19
  • 26
  • Is is like this? var currentDate = new Date(); var birth_date = new Date(actualDob); var currentYear = currentDate.getFullYear(); var myBirthYear = birth_date.getFullYear(); var myAge = currentYear - myBirthYear; document.getElementById("age").innnerHTML = myAge; – IslandKing Mar 16 '14 at 07:04
  • It's not displaying here: var myAge = currentYear - myBirthYear; document.getElementById("age").innnerHTML = myAge; in the HTML page – IslandKing Mar 16 '14 at 07:07
  • @IslandKing document.getElementById("age").value=myAge – Vinod Mar 16 '14 at 07:20
  • @Vinod Not that simple. Consider the following: on Jan 2nd, 2018, your code has been run on a birth date Aug 01, 1980. The person's age is still 37 by our rules on Jan, 2nd, 2018, but your code will tell the age as 38 already. – Matvey Andreyev Apr 05 '18 at 11:48
0

in js

// Make a button that display the current date and time in local format on the page. 


function mydateis(){
    const d = new Date();
let text = d.toLocaleString();
document.getElementById("date").innerHTML = text;



var year_born = prompt("Please enter your date of birth:", 1998);
var month_born = prompt("Please enter your month:", 1);
var month_day = prompt("Please enter your day:", 1);
 

function getAge(birthYear,month_born,month_day){
    var currentDate = new Date();
    var currentYear = currentDate.getFullYear();
    var currentmonth = currentDate.getMonth();
    var currentday = currentDate.getDate();
    console.log(currentDate);
    console.log(currentDate.getDate());
    age = currentYear - birthYear;
    month = currentmonth - month_born;
    day = currentday - month_day;
    return age,month,day;
}

calculatedAge = getAge(year_born,month_born,month_day);
 document.getElementById("yearsold").innerHTML ="you have yeyre is" + age+ "  and  "+month+"  month and  days is  "+day ;


}

in html

<button onclick="mydateis()">date is </button>
<h4>hekoo date is </h4>
<p id="date"></p>
<p id="yearsold"></p>

the out bot is enter image description here