Friday, 29 August 2014

How to Calculate Age of person from the Date of birth in Asp.Net

Calculate Birthdate


Description:-

In this example we explain that how to calculate the Age of the person from the date of the birth in asp.net. or Calculate Age based on date and display it as Days and Months using asp.net C#.
 or calculate the age of person and retrieve the year month and day depend on the user enter his birth date in textbox in 
asp.net.

sometime we have requirement in our application like in Government examination system suppose user enter his birth date then application will automatically calculate his Age and if Age is Greater then it's criteria then it will display proper message that you are not eligible for this exam.

To show Example How to insert,update,delete in DataList control clikck here  CRUD operation in DataList 

 How to Bind Excelsheet Data to Gridview CRUD operation in Excelsheet Database

So if you have to define this type of criteria in your application you should follow the steps

Default3.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to Calculate Age of Birth date of the user</title>
    <script type="text/javascript">

function Calculate_Birthdate(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(); //fetch the number of year
var months=dateNow.getMonth()-birthdayDate.getMonth(); //fetch the number of month
var days=dateNow.getDate()-birthdayDate.getDate(); //fetch the number of days
if (isNaN(years)) {

document.getElementById('lblltoalage').innerHTML = '';
document.getElementById('lblError').innerHTML = 'plz enter date in proper format!';
return false;

}

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

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



0 comments:

Post a Comment