JavaScript calculate age from date of birth

Ads
 

JavaScript calculate age from date of birth

How to calculate age from date of birth?
View Answers

May 16, 2012 at 3:37 PM

<html>
<head>
<script type="text/javascript">
    function ageCount() {
        var date1 = new Date();
        var  dob= document.getElementById("dob").value;
        var date2=new Date(dob);
        var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/; //Regex to validate date format (dd/mm/yyyy)
        if (pattern.test(dob)) {
            var y1 = date1.getFullYear(); //getting current year
            var y2 = date2.getFullYear(); //getting dob year
            var age = y1 - y2;           //calculating age 
            document.write("Age : " + age);
            return true;
        } else {
            alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
            return false;
        }

    }
</script>
</head>
<body>
Date of Birth(dd/mm/yyyy):
<input type="text" name="dob" id="dob" />
<input type="submit" value="Age" onclick="ageCount();">
</body>
</html>

Description: Here you can use regular expression to validate date format. If inputted date of birth is in form of dd/mm/yyyy then count age by subtracting dob into current date. you can also take specified date in place of current date to count age. getFullYear() is used here to get year of specified date.

Ads









Related Tutorials/Questions & Answers:
JavaScript calculate age from date of birth
JavaScript calculate age from date of birth  How to calculate age from date of birth?   <html> <head> <script type="text/javascript"> function ageCount() { var date1 = new Date
How automatically calculate age based on date of birth and current date using jsp and servlet?
How automatically calculate age based on date of birth and current date using jsp and servlet?  when user enters the Date of birth in one textbox then automatically age will be display on another textbox. event:Onlick Event
Advertisements
Determining the actual age from date of birth in Java
Determining the actual age from date of birth in Java... your complete date of birth (year, month and day of month) and gives you the exact... any invalid year or month or day for your date of birth.   Here
Calculate an average Age of my Class?
Calculate an average Age of my Class?  average age of my class
ModuleNotFoundError: No module named 'date_calculate'
ModuleNotFoundError: No module named 'date_calculate'  Hi, My... named 'date_calculate' How to remove the ModuleNotFoundError: No module named 'date_calculate' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'date_calculate'
ModuleNotFoundError: No module named 'date_calculate'  Hi, My... named 'date_calculate' How to remove the ModuleNotFoundError: No module named 'date_calculate' error? Thanks   Hi, In your python
How to calculate Date Difference in Java?
How to calculate Date Difference in Java?  Hi, I have two dates variable. How to calculate Date Difference in Java? Thanks   Hi, You can use the Calendar class for calculating the date difference in Java. Get data
JavaScript Date Difference
JavaScript Date Difference...() that returns the time (in milliseconds) elapsed from 1st January, 1970 to the current Date...;Date Difference</h2> <script type="text/javascript"> var
javascript questions on date
javascript questions on date  how to add some days to an existing date
javascript tomorrow date.
javascript tomorrow date.  Hi, how can I display tomorrow's date using javascript?   <html> <head> <title>Date after current date</title> <script type="text/javascript"> var mydate= new
javascript yesterday date.
javascript yesterday date.  Hi, How can I display the yesterday's... date</title> <script type="text/javascript"> var mydate= new Date...;/head> </html> Description: In javascript whenever you write new Date
javascript date difference in years
javascript date difference in years  I want to find the difference between two dates with respect to years. I am using javascript. Please help.   <html> <head> <script type="text/javascript"> var
Mysql Date from Date
Mysql Date from Date       Mysql  Date from Date Time in Mysql return the Current  date. Understand with Example The Tutorial illustrate an example from 'DateTime
javascript date comparison
javascript date comparison  How to compare dates and display... language="JavaScript"> var date1 = new Date("jan, 25, 2010"); var today = new Date(); var difference = today - date1; var days =parseInt(difference
javascript current date and time.
javascript current date and time.  How can we display current date and time using javascript?   <html> <head> <title>Current date and time</title> </head> <script type="text/javascript
javascript date difference in months
javascript date difference in months  How can I find the difference...; <head> <script type="text/javascript"> var date1=new Date(); var...; Description: In javascript whenever you write new Date() it shows the current date
javascript date difference in days.
javascript date difference in days.   I want to find the difference between two dates in terms of days. I am using javascript. Please help.   <html> <head> <script type="text/javascript"> var date1
JavaScript Validate UK Date
JavaScript Validate UK Date  How to Validate UK Date in JavaScript... if (!validformat.test(input.value)) alert('Invalid Date Format. Please correct.') else{ //Detailed check for valid date ranges var
date format in javascript
date format in javascript  display date format as dd/mm/yyyy and day... help me. thank u var date=/^\d{2}\/\d{2}\/\d{4}$/ if(document.userform.joindate.value.search(date)==-1) { alert("please enter date format as dd/mm/yyyy
vaidate a date from datepicker in javasript
vaidate a date from datepicker in javasript  validation should be the user should be alerted wen a future date is selected..validation can be done in javascript or jsp itself
JavaScript Show Date
JavaScript Show Date..., we are going to display the current date using JavaScript. You can see... Date</h2> <script type="text/javascript"> var date = new
age calculator
age calculator  sir , actually m working on a project where i want the user to enter his date of birth in a jtextfield .and on doing so his age should b displayed in the next textfield automatically....plz help
JavaScript parse date
JavaScript parse date..., you will learn how to parse the date in JavaScript. JavaScript provides the function parse() that takes a date in string format and returns the equivalent
sql query to get name,age,phone,address from tables t1,t2,t3,t4,t5 ..
sql query to get name,age,phone,address from tables t1,t2,t3,t4,t5 ..   this is for single table : query ="SELECT name , age , pwd , phone... by name"; i want for 12 tables : i want to fetch name age pwd phone address from 12
javascript date validation using regex
javascript date validation using regex  Hi, I want to validate the date using javascript. Please help.   <html> <head> <title>Validating Date format</title> <script type="text/javascript">
Learning JavaScript from beginning
Learning JavaScript from beginning  Hi, I want to learn HTML and JavaScript from beginning. What will be the best steps to learn these languages? Thanks   Learning JavaScript from beginning JavaScript is an easy
display date to jsp from database
display date to jsp from database   display date to jsp from database to calender if the start date and end date is available than calender date will disappeared or non-clickable and its show in red color. those date
JavaScript calendar date picker
: bold; } .style1 { color: #0000FF; } JavaScript calendar date picker We are going to discus about JavaScript calendar date picker. We have used basic JavaScript date picker functionality. We use drop-down calendar
fetch data from database in javascript
fetch data from database in javascript   How to fetch data from database in JavaScript when it is a leap year
passing value from javascript popup
passing value from javascript popup  How to take user input and pass that input value using popup in Javascript
JavaScript check if date is between two dates.
JavaScript check if date is between two dates.  Check if date is between two dates?   <html> <head> <title>Date before...: -This example is taking 2 dates from user and one date to check either

Ads