Calculate age from date of birth using java script and ajax calender in asp.net

Manimaddu
Posted by Manimaddu under JavaScript category on | Points: 40 | Views : 2758
Hi All...
Here I want to share the below code. Which explains calculate age from date of birth in the form of "years months days" format.

aspx page:

<table>
<tr>
<td>
<asp:Label ID="lblDateOfBirth" runat="server" Text="Date Of Birth"></asp:Label>
<asp:Label ID="lblMandatoryDOB" runat="server" Text="*" Font-Bold="True" Font-Size="Large" ForeColor="Red" ToolTip="Required Field"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtDateofBirth" runat="server" placeholder="Date Of Birth" Width="165px"></asp:TextBox>
<asp:ImageButton ID="ImgCalander" runat="server"
ImageUrl="~/images/Calendar_scheduleHS.png" onkeypress="javascript:getAge()" Width="21px" />
<ajax:CalendarExtender ID="ajaxCalender" runat="server" TargetControlID="txtDateofBirth" PopupButtonID="ImgCalander"></ajax:CalendarExtender>
</td>
</tr>
</table>


Java script code:


function getAge() {
var inputdob = document.getElementById('<%=txtDateofBirth.ClientID %>').value;
if (inputdob == "") {
alert("Enter date of Birth");
document.getElementById('<%=txtAge.ClientID %>').value = "";
return false;
}
var DOBDate = new Date(inputdob);
var currentDate = new Date();
var months = currentDate.getMonth() - DOBDate.getMonth();
var years = currentDate.getFullYear() - DOBDate.getFullYear();
var days = currentDate.getDate() - DOBDate.getDate();
if (years <= 0) {
alert("Date of Birth is incorrect");
return false;
}
else if (months < 0) {
years = parseInt(years, 10) - 1;
months = 12 + parseInt(months, 10);
if (days < 0) {
months = parseInt(months, 10) - 1;
days = 30 + parseInt(days, 10);
document.getElementById('<%=txtAge.ClientID %>').value = years + ' Years ' + months + ' Months ' + days + ' Days';
}
else
document.getElementById('<%=txtAge.ClientID %>').value = years + ' Years ' + months + ' Months ' + days + ' Days';
}
else {
if (months == 0) {
years = parseInt(years, 10) - 1;
if (days < 0) {
dayDiff = 30 + parseInt(days, 10);
document.getElementById('<%=txtAge.ClientID %>').innerHTML = years + ' Years ' + months + ' Months ' + days + ' Days';
}
else
document.getElementById('<%=txtAge.ClientID %>').value = years + ' Years ' + months + ' Months ' + days + ' Days';
}
else {
if (days < 0) {
months = parseInt(months, 10) - 1;
days = 30 + parseInt(days, 10);
document.getElementById('<%=txtAge.ClientID %>').value = years + ' Years ' + months + ' Months ' + days + ' Days';
}
else
document.getElementById('<%=txtAge.ClientID %>').value = years + ' Years ' + months + ' Months ' + days + ' Days';
}
}
}


Thank You..

Comments or Responses

Posted by: Ayurvedvishva on: 4/18/2016 Level:Starter | Status: [Member] | Points: 10
There is error when we select date in current year....

E.g. if Today is 18 April 2016 And I select 17 April 2016...

Out put is = -1 Years 0 Months 1 Days Instead of = 0 Years 0 Months 1 Days

I have changed
if (years <= 0) {

alert("Date of Birth is incorrect");
return false;
}


TO

 if (years < 0) {

alert("Date of Birth is incorrect");
return false;
}

Login to post response