Calculate AGE based on Date of birth using jQuery Calendar

Hi guys, Bushan here, welcome back to B2 Tech! Today in this video, I will show you how to calculate Age based on Date of birth using jQuery Calendar so let’s get started! If you are creating any kind of registration form, having fields like Date of birth and Age is very very common, the user will enter the date of birth and we need to calculate the age based on the date of birth, we should not allow the user to enter Age manually that’s a bad idea. Age will always depend on the date of birth so today in this article let’s find out how to achieve that.



The first step is to create a new HTML file, jqueryAgeCalculator.html and add all the boilerplate code

The second step is to add 3 libraries to HTML page inside the head tag, you can download all these libraries from the below link,

jQuery core library – Download
jQuery UI style library – Download
jQuery UI javascript library – Download



The third step is to create 2 textboxes and give an ID to each textbox.

<body>
    Enter Date of Birth: <input type="text" id = "dob" /><br/><br/>
    Age: <input type="text" id = "age" readonly/>
</body>

The fourth step is to add this jquery code inside the script tag, to calculate the Age

<script type="text/javascript">
    $(document).ready(function() {
        var age = "";
        $('#dob').datepicker({
            onSelect: function(value, ui) {
                var today = new Date();
                age = today.getFullYear() - ui.selectedYear;
                $('#age').val(age);
            },
            changeMonth: true,
            changeYear: true
        })
    })
</script>

Here is the complete source code,



 jqueryAgeCalculator.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="jquery-ui.css" />
    <script src="jquery.js"></script>
    <script src="jquery-ui.js"></script>
    <script type = "text/javascript">
        $(document).ready(function () {
            var age = "";
            $('#dob').datepicker({
                onSelect: function (value, ui) {
                    var today = new Date();
                    age = today.getFullYear() - ui.selectedYear;
                    $('#age').val(age);
                },
                changeMonth: true,
                changeYear: true
            })
        })
    </script>
</head>
<body>
    Enter Date of Birth: <input type="text" id = "dob" /><br/><br/>
    Age: <input type="text" id = "age" readonly/>
</body>
</html>

This is how you can calculate Age based on Date of birth using jQuery Calendar, I hope this article is helped you in one or the other way if you like this article share this with your friends. Thank you so much for reading, I will see you in the next article.

Download the file,



jqueryAgeCalculator.html – Download

About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

7 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *