Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Calculate age of person from date of birth using JavaScript

Oct 26, 2010
Introduction

Here I will explain how to calculate age of person from date of birth and regular expression for mm/dd/yyyy format using JavaScript.

Description

In some situation we need to display age of person based on date of birth .In that situation we can calculate age of person using JavaScript by comparing present date and date of birth as shown below. Here I have a one textbox every time I need to validate that data whether user enter valid data or not if the date not in mm/dd/yyyy format or not.
Regular expression for mm/dd/yyyy date format is

re=/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;

If you want to implement code for calculate age of person using javascript design your aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Calculate Age</title>
<script type="text/javascript">

function CalculateAge(birthday) {

var re=/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;

if (birthday.value != '') {

if(re.test(birthday.value ))
{
birthdayDate = new Date(birthday.value);
dateNow = new Date();

var years = dateNow.getFullYear() - birthdayDate.getFullYear();
var months=dateNow.getMonth()-birthdayDate.getMonth();
var days=dateNow.getDate()-birthdayDate.getDate();
if (isNaN(years)) {

document.getElementById('lblAge').innerHTML = '';
document.getElementById('lblError').innerHTML = 'Input date is incorrect!';
return false;

}

else {
document.getElementById('lblError').innerHTML = '';

if(months < 0 || (months == 0 && days < 0)) {
years = parseInt(years) -1;
document.getElementById('lblAge').innerHTML = years +' Years '
}
else {
document.getElementById('lblAge').innerHTML = years +' Years '
}
}
}
else
{
document.getElementById('lblError').innerHTML = 'Date must be mm/dd/yyyy format';
return false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Date of Birth :<asp:TextBox ID="txtAge" runat="server" onblur="CalculateAge(this)" />(mm/dd/yyyy)
<span style="color: Red">
<asp:Label ID="lblError" runat="server"></asp:Label></span>
<br />
Age&nbsp;&nbsp;&nbsp; : <span id="lblAge"></span>
</div>
</form>
</body>
</html>
Demo 



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

19 comments :

Unknown said...

but it isnt working in master page.what to do.please help

Suresh Dasari said...

hi,
it contains only JavaScript code it will work in any page. Please check your code i think there is mistake in your masterpage.

Chirag Makwana said...

hi i am working on C#.Net Desktop Application.
I want to calculate person age using C#.net win application.. please help me..

My email is : chirag.makwana1989@gmail.com

Thanks In Advance.

Anonymous said...

Could you explain the regular expression in depth please?

Abhishek said...

There is a bug in your code. It only subtracts year from year, months from months, and days from days. This is not how you calculate age.
For Example: If you put a day which is greater(in value) than today's date then you get negative days as number of days.
I like your other posts but this is very wrong.

Abhishek said...

if(months < 0 || (months == 0 && days < 0)) {
years = parseInt(years) -1;

document.getElementById('lblAge').innerHTML = years;
}

else {
document.getElementById('lblAge').innerHTML = years;
}

To make this code work add this.
Thanks.

Suresh Dasari said...

@Abhishek...
Thanks for pointing my mistake I updated code....

Anonymous said...

provide me download option for this tutorials

nephy said...

how to store the age in database.and how the age will increase as the year passes by.

sathyasharepoint said...
This comment has been removed by the author.
sathyasharepoint said...

Hi Suresh,
How to display the age within the textbox and how to display age automatically within the textbox without clicking on texbox..

Anonymous said...

months and days not calculated

sathish venkat said...

Hai suresh send me fromdate less than to date in javascript

Anonymous said...

it's not display month and day
please upload update code..

Svreddy Syamala said...

Dear Sir ,

Please see below link

http://www.elevateweb.co.uk/image-zoom

that type of zoom i want in datalist control imagebutton i mean imagebutton in datalist control when focus on image above link zoom i need please give me your help

Svreddy Syamala said...

Dear Sir ,

Please see below link

http://www.elevateweb.co.uk/image-zoom

that type of zoom i want in datalist control imagebutton i mean imagebutton in datalist control when focus on image above link zoom i need please give me your help

Svreddy Syamala said...

Dear Sir ,

Please see below link

http://www.elevateweb.co.uk/image-zoom

that type of zoom i want in datalist control imagebutton i mean imagebutton in datalist control when focus on image above link zoom i need please give me your help

sir its a project to me please respond immidiatly i am waiting for response

thanks & Regards
s venkata reddy

Prince Rajput said...

$(function() {
$('#bdate').datepicker({
onClose: function() {
var birthday = $(this).datepicker('getDate');
var today = new Date(),

age = (
(today.getMonth() > birthday.getMonth())
||
(today.getMonth() == birthday.getMonth() && today.getDate() >= birthday.getDate())
) ? today.getFullYear() - birthday.getFullYear() : today.getFullYear() - birthday.getFullYear()-1;
var months=today.getMonth()-birthday.getMonth();
if(months<0) months+= 11;
var days=today.getDate()-birthday.getDate();
if(days<0){
birthday.setMonth(birthday.getMonth()+1, 0);
days= birthday.getDate()-birthday.getDate()+today.getDate();
--months;
}

alert(months);
alert(days);
$('#age').val(age);
// alert("Age: " + age);
//alert(dayDiff);
}
});
});




function checkpassword()
{
var pass2 = document.getElementById("pass").value;
var cpass1 =document.getElementById("conpass").value;

if(pass2 === cpass1)
{
$('#cpass').html("");
}
else
{
document.getElementById("cpass").innerHTML="wrong password";
document.getElementById("conpass").focus();
}
}


function usercheck()
{
alert('hi');
var name = document.getElementById("uname").value;
if(name=="")
{
document.getElementById("disp").innerHTML="";
}
else
{

$.ajax({
type:"POST",
url:"checkuser.php",
dataType: "html",
data:{"name":name},
success: function(html){
$('#disp').html(html);
}
});

return false;
}
}

Lakhpat Singh said...

Dear Sir,
Display result is wrong from 28 Feb and 1st March to till date. if you have any correct code please send me on my email: maclibrary40@gmail.com.
Thanks in advance,
From: Lakhpat Singh

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.