Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i am trying to calculate the age of a person as per value given in text box. Here is my code:
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 month=dateNow.getMonth() - birthdayDate.getMonth();
        var days=dateNow.getDate() - birthdayDate.getDate();
        if(isNaN(years)){
        document.getElementById('lblage').innerHTML= '';
        document.getElementById('lblerror').innerHTML='Input data is Incorrect';
        return false;
        }
        else
        {
        document.getElementById('lblerror').innerHTML='';
        if(month<0||(month==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 in mm/dd/yyyy format';
        return false;
        }
        }
        }



Here is my Html Code
ASP.NET
<body>
    <form id="form1" runat="server">
    <div>
    Date of Birth:<asp:TextBox ID="txt1" onblur="calculateage(this)" runat="server">(mm/dd/yyyy)
</asp:TextBox>
    <span style="color:Red">
    <asp:Label ID="lblerror" runat="server"></asp:Label>
    </span>
    <br />
    Age:     :<span id="lblage"></span>
    </div>
    </form>
</body>

But when i leave the textbox after input date, It does not give anything.
Please help
Posted
Updated 24-Dec-20 1:50am

Hi,
Try this,
Javascript:
JavaScript
function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(Date.parse(dateString.value, "MM/dd/yyyy"));
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    document.getElementById("lblage").innerHTML = age;
}
}

HTML:
ASP.NET
<form id="form1"  runat="server">
    <div>
        Date of Birth:<asp:TextBox runat="server" ID="txt1" onblur="getAge(this)">MM/dd/yyyy
        </asp:TextBox>
        <span style="color: Red">
            <asp:label id="lblerror" runat="server"></asp:label>
        </span>
        <br />
        Age: :<span id="lblage"></span>
    </div>
    </form>



--Amit
 
Share this answer
 
v2
Comments
_Raj sinha 12-Aug-12 0:37am    
Hi Amit, I have tried your given code but its not working.
Gee Varghese 25-Oct-13 22:37pm    
Hello, he you are getting date in MM/dd/yyyy .
How i it possible to get date in dd/MM/yyyy
_Amy 12-Aug-12 0:52am    
Try the updated answer. Now it'll work out.
_Raj sinha 12-Aug-12 1:26am    
hii Amit, thanx for replying now your given code is working.But I want to calculate month and day along with age. can you help?
_Amy 12-Aug-12 1:30am    
There only you can calculate that. Read the better article from the net. If you'll try yourself then only you can enhance your skills and problem solving ability. All the best. :)




function calculate_age()
{
var birth_date=new Date()(document.getElementByID("birth_date").value);
var birth_date_day= birth_date.getDate();
var birth_date_month=birth_date.getMonth();
var birth_date_year=birth_date.getFullYear();

var today_date=new Date();
var today_day=today_date.getDate();
var today_month=today_date.getMonth();
var today_year=today_date.getFullYear();

var calculated_age=0;

if (today_month> birth_date_month) calculated_age=today_year-birth_date_year;
else if(today_month == birth_date_month)
{
if (today_day>= birth_date_day) calculated_age=today_year-birth_date_year;
else calculated_age= today_year-birth_date_year-1;
}
else calculated_age=today_year-birth_date_year-1;

var calculated_age=today_year-birth_date_year;
var output_value=calculated_age;
document.getElementById("calculated_age").innerHTML=calculated_age;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900