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.

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

                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'mm/dd/yy',
                    //firstDay: 1,
                    onSelect: function(dateText, inst) {
                        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); 


            DOB (mm/dd/yyyy):
            <input type="text" id="datepicker" value=''>
            <input id="AGE" type="text" value="">



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

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

    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:

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');

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)

    <script type="text/javascript">
        $(function() {
                changeMonth: true,
                changeYear: true,
                dateFormat: 'mm/dd/yy',
                //firstDay: 1,
                onSelect: function(dateText, inst) {
                    var d = new Date(Date.parse(inst.lastVal));
                    var diff = (new Date()).getFullYear() - d.getFullYear();
                    document.getElementById('AGE').value = diff;

Try This Code very simple

function agefinding()
        var birthDay = document.getElementById("TxtDOB").value;
        var DOB = new Date(birthDay);
        var today = new Date();
        var age = today.getTime() - DOB.getTime();
        age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));
       // alert(age);
        return age;

this is very simple to find the age using javascript


Assuming you construct DOB properly, create:

now = Date() //right now

use the date.getTime() function to get time in milliseconds. Divide the difference of them by by the milliseconds in a

one_year = 1000*60*60*24*365;
years = (now.getTime() - DOB.getTime())/one_year;

Put this in a callback for any change in any of your date fields and display the result wherever you like.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.