Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting age automatically when given Date of Birth

I wanted to get the age of a person from his date of birth. I have a HTML code where I'm using a datepicker for dob, when I give the date-of-birth it show automatically show the age without giving submit. Please help me in finding a solution for this.

<html>
    <head>
        <title>
            DOB calculations
        </title>
        <link rel='stylesheet' type='text/css' href='jquery-ui.css' />
        <script type='text/javascript' src='jquery.min.js'>
        </script>
        <script type='text/javascript' src='jquery-ui.min.js'>
        </script>
        <script type='text/javascript' src='jquery.js'>
        </script>
        <script type='text/javascript' src='jquery-ui-custom.js'>
        </script>
        <script type='text/javascript' src='jquery.ui.datepicker.js'>
        </script>
        <script type="text/javascript">
            $(function() {

                $('#datepicker').datepicker({
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'mm/dd/yy',
                    //firstDay: 1,
                    onSelect: function(dateText, inst) {
                        dateText.split('/');
                        Bdate = new Date(dateText[2], dateText[0] - 1, dateText[1]);
                        BDateArr = ('' + Bdate).split(' ');
                        //document.getElementById('DOW').value = BDateArr[0]; 
                        Cdate = new Date;
                        CDateArr = ('' + Cdate).split(" ");
                        Age = CDateArr[3] - BDateArr[3];
                        document.getElementById('AGE').value = Age;
                        // DOBcalc(dateText); 
                        //DOBcalc(); 
                    }
                })

            });
        </script>
    </head>

    <body>
        <form>
            DOB (mm/dd/yyyy):
            <input type="text" id="datepicker" value=''>
            Age:
            <input id="AGE" type="text" value="">
        </form>
    </body>
</html>
like image 586
yopirates Avatar asked Nov 11 '10 05:11

yopirates


People also ask

How do I find out age from birthdate?

Ans: To find out a person's age, all you need is that person's year of birth. After this, all you need to do now is subtract the birth year from the ongoing current year and you will have the age. This will help you to calculate the age from the date of birth. Age= 2020- 1966 = 54.

How do you find your original age?

The method of calculating age involves the comparison of a person's date of birth with the date on which the age needs to be calculated. The date of birth is subtracted from the given date, which gives the age of the person. Age = Given date - Date of birth.

How do you automatically enter age in Excel?

=(YEAR(NOW())-YEAR(A2)) The result is the age of person—the difference between today and the birthdate in A2. This example uses the YEAR and NOW functions. If this cell doesn't display a number, ensure that it is formatted as a number or General. Learn how to format a cell as a number or date.


3 Answers

You can try using this function as your onSelect event handler instead:

$('#dob').datepicker({
    onSelect: function(value, ui) {
        var today = new Date(),
            dob = new Date(value),
            age = new Date(today - dob).getFullYear() - 1970;

        $('#age').text(age);
    },
    maxDate: '+0d',
    yearRange: '1920:2010',
    changeMonth: true,
    changeYear: true
});

This should be very accurate (and much better than my old code), since everything's been handed off to the native Date object.

See a simple demonstration of this here: http://www.jsfiddle.net/yijiang/PHvYK/1

like image 106
Yi Jiang Avatar answered Sep 22 '22 10:09

Yi Jiang


function getAge(birth) {
   var today = new Date();
   var curr_date = today.getDate();
   var curr_month = today.getMonth() + 1;
   var curr_year = today.getFullYear();

   var pieces = birth.split('/');
   var birth_date = pieces[0];
   var birth_month = pieces[1];
   var birth_year = pieces[2];

   if (curr_month == birth_month && curr_date >= birth_date) return parseInt(curr_year-birth_year);
   if (curr_month == birth_month && curr_date < birth_date) return parseInt(curr_year-birth_year-1);
   if (curr_month > birth_month) return parseInt(curr_year-birth_year);
   if (curr_month < birth_month) return parseInt(curr_year-birth_year-1);
}

var age = getAge('18/01/2011');
alert(age);
like image 28
paulinho Avatar answered Sep 22 '22 10:09

paulinho


If you subtract two Date objects in javascript, you get their difference in milliseconds so ( BDate - (new Date()) )/365.25/24/60/60/1000 will give you an age result that should be accurate to within a day. (i.e. if their birthday is today and its a leap year it may be inaccurate)

like image 26
tobyodavies Avatar answered Sep 23 '22 10:09

tobyodavies