calculate the age by giving 2 dates

Posted by Chaithragm under ASP.NET on 8/21/2013 | Points: 10 | Views : 2003 | 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

Up
0
Down
USE DATEDIFF function in C#
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/DateDiff_CS_DC09132006172429PM/DateDiff_CS_DC.aspx
http://forums.asp.net/p/1082692/1604940.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 8/21/2013 [Member] Starter | Points: 25

Up
0
Down
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

Up
0
Down
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-Mo

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/26/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Sample code for number of days between DOB and default date
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CalculateAge.aspx.cs" Inherits="WebApplication1.CalculateAge" %>


<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtDate" runat="server" />
<asp:Button Text="Click" runat="server" id="btnAge" onclick="btnAge_Click"/>
<asp:Label Text="" ID="lblAge" runat="server" />
</div>
</form>
</body>
</html>


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.gif

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response