1

My question is how to get the age from the date input type using Javascript, and show it in Html.

I want the user to be able to select his Date of Birth from the Date input type, which should then be put into the 'bday' ID that I can then use to calculate the age from and fill it in the 'resultBday' element ID.

For example:
When the user selects 'January 15th, 1990' I want it to show "24 years old" in the innerHtml.

This is what I currently have:

HTML:

<p id="resultBday"></p>
<input type="date" name="bday" id="bday" onchange="submitBday()">

JS:

function submitBday () {
    var Q4A = "Your birthday is: "
    var Bday = document.getElementById('bday').value;
    Q4A += Bday;

    var theBday = document.getElementById('resultBday');
    theBday.innerHTML = Q4A;
}
10
  • 1
    So what's the question? Commented Jan 24, 2014 at 15:56
  • 1
    Surely it's How to get the 'age' from input type 'date' using html & js?? I.e. the title? Commented Jan 24, 2014 at 15:56
  • But the code works... Commented Jan 24, 2014 at 15:57
  • 1
    @Cristy - the code just shows the selected date, it doesn't calculate the age. Commented Jan 24, 2014 at 15:58
  • 2
    Oh, so the question actually is "How to get age from birthdate..." Commented Jan 24, 2014 at 15:59

3 Answers 3

6
function submitBday() {
    var Q4A = "Your birthday is: ";
    var Bdate = document.getElementById('bday').value;
    var Bday = +new Date(Bdate);
    Q4A += Bdate + ". You are " + ~~ ((Date.now() - Bday) / (31557600000));
    var theBday = document.getElementById('resultBday');
    theBday.innerHTML = Q4A;
}

jsFiddle example

Sign up to request clarification or add additional context in comments.

Comments

1

A simple way to calculate the Year and month is as below:

HTML:

<label>Your Date of Birth <label>
<input type="date" name="birthday" id='birthday' onchange="ageCount()"> 

<p id="demo">age shows here</p>

JavaScript

<script>
  function ageCount() {
    var now =new Date();                            //getting current date
    var currentY= now.getFullYear();                //extracting year from the date
    var currentM= now.getMonth();                   //extracting month from the date
      
    var dobget =document.getElementById("birthday").value; //getting user input
    var dob= new Date(dobget);                             //formatting input as date
    var prevY= dob.getFullYear();                          //extracting year from input date
    var prevM= dob.getMonth();                             //extracting month from input date
      
    var ageY =currentY - prevY;
    var ageM =Math.abs(currentM- prevM);          //converting any negative value to positive
      
    document.getElementById('demo').innerHTML = ageY +' years ' + ageM +' months';
    }
    
    </script>

Comments

-1
get ageData(){
      const today = new Date();
      const newDate = new Date(this.dateOfBirth)
      const age = today.getFullYear() - newDate.getFullYear()
      console.log(newDate.getFullYear());
      return age;
    }
    return ageDate

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.