Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I have 2 Textbox. First is Date of Birth and Second is Age.

When I enter Date of Birth in 1st Textbox1 then Automatically Calculate Age in 2nd Textbox.

Can Anyone Please Help me/
How to Calculate Age Without JavaScript?

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Age.aspx.cs" Inherits="Percentage" %>

<!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>
        <table>
            <tr>
                <td>
                    DOB
                </td>
                <td>
                    <asp:TextBox ID="txtDOB" runat="server" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Age</td>
                <td>
                    <asp:TextBox ID="txtAge" runat="server" "></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Calculate" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
Posted

Here try this:
In your Button's OnClick event

C#
DateTime bday = Convert.ToDateTime(txtDOB.Text);
           DateTime today = DateTime.Today;
           int age = today.Year - bday.Year;
           if (bday > today.AddYears(-age)) age--;

           txtAge.Text = age.ToString();


Hope it helps

Azee...
 
Share this answer
 
v2
Comments
Arpit6016 3-Oct-13 0:09am    
Thank You.
This Code Is Perfectly Work.
It's a pain, isn't it?

See here: Working with Age: it's not the same as a TimeSpan![^]
 
Share this answer
 
Well,this answer could be numerous! Yes, age calculation from dob could be a number of ways. Absolutely that depends on which format you are taking input from user. The simplest one could be like:
C#
var today = float.Parse(DateTime.Now.ToString("yyyy.MMdd"));
var inputDob = float.Parse(dateOfBirth.ToString("yyyy.MMdd"));
var age = (int)(today - inputDob);

Where dateOfBirth is your input.
And i would recommend you to see this link where there are some excellent alternatives.See which one suits you:
How do I calculate someone's age in C#[^]
 
Share this answer
 
check this . it is helpful.
 
Share this answer
 
Add the following code in your aspx page
ASP.NET
<asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox>
        <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown"></asp:textbox>
    <asp:panel runat="server" id="Panel1" xmlns:asp="#unknown">
                <asp:calendar id="Calendar1" runat="server" onselectionchanged="PickDate" forecolor="Green"></asp:calendar>
                </asp:panel>

Add a class to your solution:
C#
public class AgeFormat
    {
        private int days = 0;
        private int months = 0;
        private int years = 0;

        public int Days
        {
            get { return days; }
            set { days = value; }
        }

        public int Months
        {
            get { return months; }
            set { months = value; }
        }

        public int Years
        {
            get { return years; }
            set { years = value; }
        }
    }

In code behind file(.cs) add the following:
C#
public void PickDate(object sender, EventArgs e)
        {
            AgeFormat ageFormat = new AgeFormat();

            int Days = Convert.ToInt32(Calendar1.SelectedDate.Day);
            int Months = Convert.ToInt32(Calendar1.SelectedDate.Month);
            int Years = Convert.ToInt32(Calendar1.SelectedDate.Year);

            this.FindAge(Years, Months, Days, ref ageFormat);

            TextBox1.Text = Calendar1.SelectedDate.Day.ToString("00") + "/" + Calendar1.SelectedDate.Month.ToString("00") + "/" + Calendar1.SelectedDate.Year.ToString("0000");
            TextBox2.Text = ageFormat.Days.ToString("00") + "/" + ageFormat.Months.ToString("00") + "/" + ageFormat.Years.ToString("0000");
            ToggleCalendar();
        }

        private void ToggleCalendar()
        {
            Panel1.CssClass = ("calendarHide" == Panel1.CssClass) ? ("calendarShow") : ("calendarHide");
            //btnDate.Text = ("Hide Calendar" == btnDate.Text) ? ("Show Calendar") : ("Hide Calendar");
        }

        private void FindAge(int years, int months, int days, ref AgeFormat ageFormat)
        {
            int cDay = DateTime.Now.Day;
            int cMonth = DateTime.Now.Month;
            int cYear = DateTime.Now.Year;

            if (cDay < days)
            {
                if (cMonth == 1 || cMonth == 3 || cMonth == 5 || cMonth == 7 || cMonth == 8 || cMonth == 10 || cMonth == 12)
                {
                    cDay = cDay + 31;
                }
                else if (cMonth == 4 || cMonth == 6 || cMonth == 9 || cMonth == 11)
                {
                    cDay = cDay + 30;
                }
                else if (cMonth == 2)
                {
                    if (cYear % 4 == 0)
                    {
                        cDay = cDay + 29;
                    }
                    else
                    {
                        cDay = cDay + 28;
                    }
                }

                cMonth--;
            }
            
            if (months > cMonth)
            {
                cMonth = cMonth + 12;
                cYear--;
            }

            ageFormat.Days = cDay - days;
            ageFormat.Months = cMonth - months;
            ageFormat.Years = cYear - years;
        }

The above methods will calculate the days, months and Years.
But if you only require the years of the age then don't hook up on method FindAge and change its definition (as ScArcher2 wrote in above mentioned link) following:
C#
private int FintAge(int years, int months, int days)
        {
            int ageYears = 0;

            double dob = ((years * 100) + months * 100) + days;
            double cDate = ((DateTime.Now.Year * 100) + DateTime.Now.Month * 100) + DateTime.Now.Day;

            ageYears = Convert.ToInt32((cDate - dob) / 10000);

            return ageYears;
        }

Wish you best of luck.
 
Share this answer
 
v4

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