Calculate age from "Date" HTML element

how to calculate age manually
calculate age from date of birth in excel
best age calculator
age calculator pearson
age difference calculator
date calculator
what is my age
medical age calculator

I have an HTML form with "Date" element and a submit button. There are 2 functions. One is for calculating age from given birth date in the "date" HTML element. The other function is for comparing the age with minimal allowed age. I need help to complete it. See my questions in remarks in code. Yes, I have seen other similar questions. I would like to learn and not just copy/paste. If I understand what is wrong with my code / syntax, then I will learn better. Thanks in advance.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0  Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title></title>
</head>
<body>
 <script type="text/javascript">

    var minAge = 18;

    var curDate = new Date();

    var curr_year = curDate.getFullYear();
    var curr_month = curDate.getMonth();


    //Calculates age from given Birth Date in the form//
    function _calcAge() {

        var dt1 = document.getElementById('date').value;
        var birth_date = new Date(dt1);

        var birth_year = birth_date.getFullYear();
        var birth_month = birth_date.getMonth();
        var calc_year = curr_year - birth_year;
        var calc_month = curr_month - birth_month;

        //The following below is what I am not sure about. 
        //I need to combine years and months and
        //convert them into a string??? Is this syntax dead wrong?

    var final_result = (calc_year && "." && calc_month).toString();

        // final result should be a number with a decimal point, example: 35.5
        final_result = parseFloat;

        return (final_result);
        alert(final_result);
    }

    //Compares calculated age with minimum age and acts according to rules//
    function _setAge() {

        var age = _calcAge();
        //alert("my age is " + age);
        if (age < minAge) {
            alert("You are not allowed into the site. The minimum age is 18!");
        } else

            alert("Welcome to my Site");
            window.open(main.htm, _self);

    }


    </script>

     <form>
      Date Of Birth: <input type="date" name="date of birth" id="date" />
      <input type="submit" name="submit" id="submit" onClick="_setAge();" />

     </form>

    </body>
   </html>

You can add or subtract javascript dates by basic mathematical functions.

Just make a new Date for today var Today = new Date() and then substract the date from the form. You get the difference in milliseconds.

Now you just need to convert them to years/months by dividing them by (1000*60*60*24*365)

So just do:

var difference = (curDate - birth_date)/((1000*60*60*24*365);
var differenceString = "The difference is: " + difference.toFixed(1) + " years.";

The toFixed(1); returns your number with 1 decimal point.

And always remember JavaScripts Dates Months are zero-based.

Your second function should work then too.

Here is a working fiddle

Age Calculators - users.med.cornell.edu, Age Calculator. Calculate the age based on the Date of Birth and another date (​default is the current date). Date of Birth. Age at This Date. Age. = years months. Search for Retirement Calculator. Whatever You Need, Whatever You Want, Whatever You Desire, We Provide.

This will work you can simply replace code with this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0  Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">

        var minAge = 18;
        function _calcAge() {
            var date = new Date(document.getElementById("date").value);
            var today = new Date();

            var timeDiff = Math.abs(today.getTime() - date.getTime());
            var age1 = Math.ceil(timeDiff / (1000 * 3600 * 24)) / 365;
            return age1;
        }
        //Compares calculated age with minimum age and acts according to rules//
        function _setAge() {

            var age = _calcAge();
            //alert("my age is " + age);
            if (age < minAge) {
                alert("You are not allowed into the site. The minimum age is 18!");
            } else

                alert("Welcome to my Site");
            window.open(main.htm, _self);

        }


    </script>
    <form>
    Date Of Birth:
    <input type="date" name="date of birth" id="date" />
    <input type="submit" name="submit" id="submit" onclick="_setAge();" />
    </form>
</body>
</html>

Calculate Age in Excel, Calculate Age. Enter the date of birth into cell A2. Enter the TODAY function into cell B2 to return today's date. The DATEDIF function below calculates the age of a person. Calculate the age of a person without displaying today's date. Calculate the age of a person on a specific date. Age Calculator. The Age Calculator can determine the age or interval between two dates. The calculated age will be displayed in years, months, weeks, days, hours, minutes, and seconds. The age of a person can be counted differently in different cultures. This calculator is based on the most common age system.

I've managed to get it to work. Credit also goes to https://stackoverflow.com/users/444991/matt for the part I borrowed from him here: https://stackoverflow.com/a/4076440/3189118

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <title></title>
   </head>
     <body>
      <script type="text/javascript">

      var minAge = 18;
      var today = new Date()
      //Calculates age from given Birth Date in the form//
       function _calcAge(birthDate, givenDate) {
        givenDate = new Date(today);
        var dt1 = document.getElementById('date').value;
        var birthDate = new Date(dt1);
        var years = (givenDate.getFullYear() - birthDate.getFullYear());

        if (givenDate.getMonth() < birthDate.getMonth() ||
     givenDate.getMonth() == birthDate.getMonth() && givenDate.getDate() < birthDate.getDate()) {
            years--;
        }

        return years;
    }

    //Compares calculated age with minimum age and acts according to rules//
    function _setAge() {

        var age = _calcAge();
        if (age < minAge) {
            alert("You are not allowed into the site. The minimum age is 18!");
        } else

            alert("Welcome to my Site");


    }


</script>

<form>
    Date Of Birth: <input type="date" name="date of birth" id="date" />
    <input type="submit" name="submit" id="submit" onClick="_setAge();" />

</form>

How to calculate age in Excel from birthday, (TODAY()-B2) returns the difference between the current date and date of birth is days, and then you divide that number by 365 to get the numbers of years. Calculate Age 1. Enter the date of birth into cell A2. 2. Enter the TODAY function into cell B2 to return today's date. 3. The DATEDIF function below calculates the age of a person. Note: fill in "y" for the third argument to get the number 4. Calculate the age of a person without displaying

Try This..

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Person Details</title>
</head>

<body>
    <form>
        Birthday: <input type="date" name="bday" id="birth_date">
        <br><br>
    </form>

    <button onclick="myFunction()">view</button><br>

    <p id="age"></p>
    <script>
        function myFunction() {


            var dobString = document.getElementById("birth_date").value;
            var calculatedValue = calculate_age(dobString);

        }

        function calculate_age(dobString) {
            var diff_ms = Date.now() - Date.parse(dobString);

            var yearString = "";
            var monthString = "";
            var dayString = "";

            var yearValue = Math.floor(diff_ms / 31536000000);
            console.log("Years ", yearValue);
            var monthValue = Math.floor((diff_ms % 31536000000) / 2592000000);
            console.log("Months ", monthValue);
            var dayValue = Math.floor(((diff_ms % 31536000000) % 2592000000) / 86400000);
            console.log("Days ", dayValue);
            // var calculatedValue = yearValue + " years " + monthValue + " months " + dayValue + " days"; // calculate and execute age

            if (yearValue > 1) yearString = " years";
            else yearString = " year";
            if (monthValue > 1) monthString = " months";
            else monthString = " month";
            if (dayValue > 1) dayString = " days";
            else dayString = " day";

            if ((yearValue > 0) && (monthValue > 0) && (dayValue > 0))
                var calculatedValue = yearValue + yearString + " " + monthValue + monthString + " " + dayValue + dayString + " ";
            else if ((yearValue > 0) && (monthValue > 0) && (dayValue == 0))
                var calculatedValue = yearValue + yearString + " " + monthValue + monthString + " ";
            else if ((yearValue > 0) && (monthValue == 0) && (dayValue > 0))
                var calculatedValue = yearValue + yearString + " " + dayValue + dayString + " ";
            else if ((yearValue == 0) && (monthValue > 0) && (dayValue > 0))
                var calculatedValue = monthValue + monthString + " " + dayValue + dayString + " ";
            else if ((yearValue == 0) && (monthValue == 0) && (dayValue > 0))
                var calculatedValue = dayValue + dayString + " ";

            document.getElementById("age").innerHTML = "Age : " + calculatedValue;

        }`

    </script>`
</body>

</html>

How to Calculate Age from a Birthdate in Excel 2013, How do I calculate age from a specific date in Excel? get or calculate age from birth date in Excel Calculate age based on date of birth with YEARFRAC function Calculate age based on date of birth with DATEDIF function Calculate age based on date of birth in years, months and days Calculate age on specific date in Excel Calculate age based on the

Use DATEDIF to Calculate Age in Excel, is =DATEDIF(birth_date,as_of_date,"y"). This returns the number of years rounded down. Other methods, such as =INT((end-start)/365.25) or =INT(YEARFRAC(start,end)) are not 100% correct. To calculate a person’s age using the DATEDIF function, you can enter a formula like this: =DATEDIF ("5/16/1972",TODAY (),"y") You can, of course, reference a cell that contains a date: =DATEDIF (B4,TODAY (),"y") The DATEDIF function calculates the number of days, months, or years between two dates.

▷ Age Calculator, ٩(^ᴗ^)۶ Calculate your exact age in details like year, month, weeks and days just by your date of birth with easy copy, print & pdf save options (best online age  Excel can help you calculate the age of a person in different ways. The table below shows common methods to do so, using the Date and time functions. To use these examples in Excel, drag to select the data in the table, then right-click the selection and pick Copy.

Age Calculator, Learn a few easy ways to calculate age in Excel: how to get age from date of birth​, age between two dates, exact age in years, months, days,  computerstaracademy,calculate age in excel,calculate age in excel in years and months and days, calculate age from date of birth in excel,calculate age from date of birth - excel functions and

Comments
  • Thanks, but... If we do this your way (and it looks good), how do we account for years that have 366 days? Can't I somehow use the build in functionality of the "date" element to account for all of that?
  • The subtraction automatically subtracts 366 days if there are any years with 366. With rounding to 1 decimal point you cover up 0.1*365 so 36.5 days of tolerance so like +/- 18 days...the possibility that there is not there until you have an age higher than 4*18 days until the one decimal might be wrong by 1 I guess. Might not cover really every case but precise enough i think. If the result is right or wrong depends on how you define a year. If the year you are calculating with is defined with 365 days its fine like this.