Click here to Skip to main content
15,559,455 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
hello friends,

please help me out , how to display the persons age,months and days while entering his date of birth in textbox (eg : 13/06/1991), should display of his current age in years, months, and days.

thanking you .
Posted
Updated 18-Dec-13 0:48am
v5
Comments
Maciej Los 18-Dec-13 5:39am    
Could you be more specific? Please, provide more details about your issue.
CPallini 18-Dec-13 5:45am    
You need an algorithm for that. :-)
Maciej Los 18-Dec-13 6:57am    
;)My virtual 5!

Read this article by OriginalGriff:
Working with Age: it's not the same as a TimeSpan!]
 
Share this answer
 
I won't give you the algorithm because that something you should be able to think about by your self

use DateTime:

DateTime today
DateTime enteredBirth

today = DateTime.Now.Date, then you have the current date.
then simply "..." enteredBirth date from today.

voila you recieve a DateTime with Years,Months,Days,hours,minutes,seconds
and this one is your Age in years, months and days
 
Share this answer
 
v2
Finally i found :
xxx.aspx

XML
<table class="style1">
                        <tr>
                            <td align="center">
                                ENTER YOUR DATE OF BIRTH</td>
                        </tr>
                        <tr>
                            <td align="center">
                                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                &nbsp;</td>
                        </tr>
                        <tr>
                            <td align="center">
                                years :
                                <asp:Label ID="lblyr" runat="server" Text="Label"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp; months :&nbsp;
                                <asp:Label ID="lblmnths" runat="server" Text="Label"></asp:Label>
&nbsp;&nbsp;&nbsp; days :&nbsp;
                                <asp:Label ID="lbldays" runat="server" Text="Label"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                               &nbsp;</td>
                        </tr>
                        <tr>
                            <td align="center">
                                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="submit" />
                            </td>
                        </tr>
                    </table>




xxx.cs

protected void Button1_Click(object sender, EventArgs e)
{

DateTime dob = Convert.ToDateTime(TextBox1.Text);
int birthmonth = Convert.ToInt32(dob.Month);

DateTime today = DateTime.Today;
int month = Convert.ToInt32(today.Month);

int age = today.Year - dob.Year;
int currentmonth = month - birthmonth;
int currentmonth2 = birthmonth - month;


if (birthmonth > currentmonth)
{
lblyr.Text = age.ToString();
lblmnths.Text = currentmonth2.ToString();

}
else
{
lblyr.Text = age.ToString();
lblmnths.Text = currentmonth.ToString();
}
}
 
Share this answer
 
v2
Comments
Pheonyx 18-Dec-13 8:50am    
Consider someone born in July 1980 and the current date being June 2014 ... your system will say they are 34 and -1 month .. that's kind of right, but you are better of saying they are 33 and 11 months. Don't you think?

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