Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
textbox1.text- DOB in format dd/mm/yyyy by user
how to calcualte age in years
Posted
Comments
[no name] 11-Aug-14 7:59am    
Current year - date of birth year = current age.
OriginalGriff 11-Aug-14 8:35am    
Not quite right: if I was born on 12 August 1996, then your formula would say:
2014 - 1996 = 18
But it'd still be illegal for me to drink until tomorrow...:laugh:
Swinkaran 11-Aug-14 8:05am    
Get the year of birth using regular expression. then find the age.
phil.o 22-Oct-15 7:08am    
Doing anything datetime-related with regular expressions is nonsense.

hey i am not an expert but try this code maybe its too lengthy and may have lots of if-nested-if loops but i think even a beginner can understand this code quickly

first i am doing this code to accept a text of date of birth in one textbox and display age on another textbox as soon as the date of birth is enetered

lets take the textbox which date of birth has to be entered as txtDateofBirth and that to display age as txtAge
so we have to display age when text is changed in txtDateofBirth


C#
protected void txtDateofBirth_TextChanged1(object sender, EventArgs e)
    {
        DateTime dob = Convert.ToDateTime(txtDateofBirth.Text); //convert the inputtext to date format ie of dd/mm/yyy
        int myYear = dob.Year;//getting year of date of birth
        int myMonth = dob.Month;//getting month of date of birth
        int myDay = dob.Day;//getting day of date of birth
        int currentYear = DateTime.Now.Year;//getting current year
        int currentMonth = DateTime.Now.Month;//getting current month
        int currentDay = DateTime.Now.Day;//getting current day
        int Age = currentYear - myYear;//calculating age in a general approach
        if (currentMonth >= myMonth)
        {
            if (currentMonth == myMonth)
            {
                if (currentDay > myDay)
                {
                    txtAge.Text = Convert.ToString(Age);
                }
                else
                {
                    txtAge.Text = Convert.ToString(Age - 1);
                }
            }


            else
            {
                txtAge.Text=Convert.ToString(Age);
            }

            }
        else
        {
            txtAge.Text=Convert.ToString(Age-1);
        }
        }
    }

so you must enable autopostback of txtDateofBirth To get the age quickly
this is my first solution so sorry if it is too lengthy
 
Share this answer
 
Comments
Patrice T 22-Oct-15 7:06am    
Don't you think the problem have been solved since a year ?
Patrice T 22-Oct-15 7:07am    
And you code is not even efficient.
CHill60 22-Oct-15 18:39pm    
"not even efficient" ... a generous comment if ever I heard one ;-)
Patrice T 22-Oct-15 18:56pm    
:-)
Annsmon Joseph 23-Oct-15 3:51am    
ha ha great
i am totally new to this world
so i am bit slow just started my coding life with asp.net
 
Share this answer
 
Comments
Maciej Los 22-Oct-15 6:53am    
+5
OriginalGriff 22-Oct-15 7:00am    
Good grief! Are you doing re-runs now? "Best of OriginalGriff, Volume III"? :laugh:
Maciej Los 22-Oct-15 7:15am    
Why volume III? Let "Best of OriginalGriff" last forever! :laugh:
Cheers, Maciej

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