JavaScript calculate age from date of birth

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.









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
Calculate Age using current date and date of birth
Calculate Age using current date and date of birth In this section you will learn how to calculate the age. For this purpose, through the code, we have prompted the user to enter current date and date of birth in a specified format. Now
Determining the actual age from date of birth in Java
Determining the actual age from date of birth in Java       Here, you can calculate... your complete date of birth (year, month and day of month) and gives you the exact
Calculate an average Age of my Class?
Calculate an average Age of my Class?  average age of my class
how can you calculate you your age in daies??
how can you calculate you your age in daies??  **hi, I am beginner in java! can any one help me to write programm to calculate age in daies...); System.out.print("Enter day of your date of birth: "); int day=input.nextInt
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 picker - Date Calendar
javascript date picker  how to insert a date picker user control in a html/jsp using javascript??? please help, it's urgent.  Hi Friend, Please visit the following links: http://www.roseindia.net/javascript
Date object using JavaScript
Date object using JavaScript  What's the Date object using JavaScript
javascript questions on date
javascript questions on date  how to add some days to an existing date
JavaScript Date Difference
JavaScript Date Difference...() that returns the time (in milliseconds) elapsed from 1st January, 1970 to the current Date...; <h2>Date Difference</h2> <script type="text/javascript
Week calculate start from friday
Week calculate start from friday  My problem is to calculate how many weeks have a month. Note that my weeks have to start on Friday! I have three combo boxes. First display year. Second one display month. Third one display
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
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
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
Mysql Date from Date
Mysql Date from Date       Mysql  Date from Date Time in Mysql return the Current ... an example from 'DateTime in Mysql'. To grasp this example, we use date(current
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
JavaScript Show Date
JavaScript Show Date..., we are going to display the current date using JavaScript. You can see in the given example, we have created Date object which holds the current date and time
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
start date and end date validation in javascript - Ajax
start date and end date validation in javascript  hi, i am doing web surfing project. pls guide me for my project. i want start date and end validations in javascript. end date should be greater than start date if so less than
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 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...; Learning JavaScript from beginning JavaScript is an easy to learn, easy
JavaScript Date Function
JavaScript Date Function Many language provide special functions for Date, JavaScript also provides many functions for it. Given examples will illustrate the functions. JavaScript Date Function Example:ADS_TO_REPLACE_1 <html>
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
how to generate the pdf file with scroolbar from jsp age - JSP-Servlet
how to generate the pdf file with scroolbar from jsp age  How to generate the pdf file with scroolbar from jsp.i am not able to see the all the columns in pdf file now .it is very urgent for me plz help
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
calculate total hours by start time and end time in javascript - Ajax
calculate total hours by start time and end time in javascript  hi, i am doing Web surfing project. i used start and end timepicker field.it works... my doubts. how to calculate total hours by using start and end time field
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
Validating date in javascript - Java Interview Questions
Validating date in javascript   Hi Deepak, Can u give sample code validating date in javascript using text box. thank u in advance  In what format you are going to enter the date in Text box (i.e : dd/mm
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
Locale Specific Date validations through JavaScript/Ajax...
Locale Specific Date validations through JavaScript/Ajax...  Hi, I....), how to manipulate date validations through JavaScript or Ajax? Thanks... JavaScript validation, but change the error/information message as per your local
Subtraction of a date from Sql and other from java.
Subtraction of a date from Sql and other from java.  I want to subtract a date that i retrieve from mysql database with the system date that i retrieve from java. I want the difference between the two date in the integer form
Getting data from servlet into javascript
Getting data from servlet into javascript  How do i get json data from my servlet on to a variable in javascript n bind the data to display onto sigma grid.Has anyone Idea how to do
update date from stored procedure
update date from stored procedure  how to works on insert,update data from stored procedure.   Insert data using stored procedure
Javascript calculate number of days between two dates
Javascript calculate number of days between two dates In this tutorial, you will learn how to calculate number of days between two dates.For this, you need to use the Date object  to retrieve the Date's millisecond value
date_parse_from_format
  date_parse_from_format date_parse_from_format function returns... date_parse_from_format ( string $format , string $date ) Parameters of Date...;25.8.2009 17:00-03:00"; print_r(date_parse_from_format("j.n.Y H:iP"
Retrieve date from MYSQL database
to retrieve the date of birth of persons from the database table. For this, we have...Retrieve date from MYSQL database In this tutorial, you will learn how to retrieve date from database. Storing and Retrieving dates from the database
how to call javascript function from flex
how to call javascript function from flex  how to call javascript function from flex
JavaScript getUTCDate method
; getUTCDate() method of JavaScript when applied to a date object returns an integer value between 1 and 31 that represents day from the date object's..._date.getUTCDate(); Where object_date is the date object from which

Ads