0

I am trying to create a form in html/javascript where the user can input their date of birth and then a message is displayed stating "you are 'x' years old", using the data from their date of birth. However I am completely unsure of how to get this to work?

Here is the code I have currently:

HTML:

<input type="date" id="myBirthday" value="dd-mm-yyyy">
<button onclick="submitBirthday()">Submit</button>

JS:

function submitBirthday() {
    var birthday = document.getElementById("myBirthday").value;
    document.getElementById("displayBirthday").innerHTML = ("You are " + birthday + " years old.");
}

I realise this displays "you are 'dd-mm-yyyy' years old currently, but this is all I've been able to do.

Any help will be greatly appreciated! Thanks

Banana
  • 7,424
  • 3
  • 22
  • 43
  • 2
    please post what you have tried so far – Banana Jan 01 '15 at 17:53
  • unction submitBirthday() { var birthday = document.getElementById("myBirthday").value; document.getElementById("displayBirthday").innerHTML =("You are " + birthday + " years old."); I realise this displays "you are 'dd-mm-yyyy' years old currently, but this is all I've been able to do. – Alex Macdonald Jan 01 '15 at 18:02

3 Answers3

0

You can find out the age by subtracting date of birth from Present date.

Please check this out.

How to subtract date/time in javascript?

Community
  • 1
  • 1
MegaMind_2
  • 111
  • 1
  • 11
0

according to This page, here is your fixed code to retrieve the age from the specified date:

function submitBirthday() {
    var minutes = 1000 * 60;
    var hours = minutes * 60;
    var days = hours * 24;
    var years = days * 365;

    var birthday = Date.parse(document.getElementById("myBirthday").value);
    var dateNow = new Date();
    var YearsOld = Math.round((dateNow-birthday)/ years);

    document.getElementById("displayBirthday").innerHTML = ("You are " + YearsOld + " years old.");
}
<input type="date" id="myBirthday" value="dd-mm-yyyy">
<button onclick="submitBirthday();">Submit</button>
<hr>
<div id="displayBirthday"></div>
Banana
  • 7,424
  • 3
  • 22
  • 43
0

Here is the code u can use to get date of birth

var input = '5/1/1991';// Find the user's input
    var dob = input.split('/'); // split the date by separator
    //console.log(dob);
    var bd = dob[0];// find birth date, month and year
    var bm = dob[1];
    var by = dob[2];

    var curDate = new Date();// find current date
    var cd = curDate.getDate();
    var cm = parseInt(curDate.getMonth(), 10) + 1;
    var cy = curDate.getFullYear();
    //console.log(cd, cm, cy);

    if (cd < bd) {
        var dayToMinus;
        switch(cm) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                dayToMinus = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                dayToMinus = 30;
                break;
            case 2:
                if (cy % 4) {
                    dayToMinus = 28;
                } else {
                    dayToMinus = 29;
                }
        }
        cd = cd + dayToMinus;
        cm = cm - 1;
    }

    if (cm < bm) {
        cm = cm + 12;
        cy = cy - 1;
    }
    console.log("You are "+ (cy - by) +" years "+ (cm - bm) + " months "+ (cd - bd) + " days old");//This will console ur age
intekhab
  • 1,566
  • 1
  • 13
  • 19