# calculate the age by giving 2 dates

Posted by Chaithragm under ASP.NET on 8/21/2013 | Points: 10 | Views : 1887 | Status : [Member] | Replies : 4
How to calculate the age by giving two dates
date1=dob of a person
date2="31-07-12";
int age =?
how to calculate age?

#### Responses

Posted by: Bandi on: 8/21/2013 [Member] [MVP] Platinum | Points: 25
Posted by: Chaithragm on: 8/21/2013 [Member] Starter | Points: 25
 0 can we do ir in asp.net by giving the default date?Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Bandi on: 8/21/2013 [Member] [MVP] Platinum | Points: 25
 0 Yes you can do... Sample app in ASP .NET to calculate Age http://www.aspdotnet-suresh.com/2010/08/calculating-difference-between-two.html http://stackoverflow.com/questions/10618207/how-would-you-calculate-the-age-in-c-sharp-using-date-of-birth-considering-leap Alternate approach is: Write this DateDifference Method and call by passing two datetime in your application http://www.codeproject.com/Articles/28837/Calculating-Duration-Between-Two-Dates-in-Years-MoMark This Response as Answer -- Chandu http://www.dotnetfunda.com/images/dnfmvp.gifChaithragm, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Bandi on: 8/26/2013 [Member] [MVP] Platinum | Points: 25
 0 Sample code for number of days between DOB and default date ```<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CalculateAge.aspx.cs" Inherits="WebApplication1.CalculateAge" %>
``` ```using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class CalculateAge : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnAge_Click(object sender, EventArgs e) { DateTime date = DateTime.Parse(txtDate.Text); DateTime defaultDate = DateTime.Parse("07-31-2012"); TimeSpan days = defaultDate.Subtract(date); double NumofYears = days.TotalDays; lblAge.Text = NumofYears.ToString() + " Days"; } } }``` Mark This Response as Answer -- Chandu http://www.dotnetfunda.com/images/dnfmvp.gifChaithragm, if this helps please login to Mark As Answer. | Alert Moderator
Latest Posts