Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How to compute age

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ad - 10 May 2005 14:31 GMT
How to compute the age of a person?
For example, a person with birthday of  1964/09/25.
How to compute his age with c#?
The Crow - 10 May 2005 14:43 GMT
simply substract two DataTime values, u will get TimeSpan object. then u can
convert it to a suitable string
Fred Mellender - 10 May 2005 14:46 GMT
Look at the DateTime.Subtract method, which will subtract two dates,
returning a TimeSpan,
which will tell you the number of years the two dates are apart.  One of
your DateTimes would be
DateTime.Now, the other the date of the person's birth.

> How to compute the age of a person?
> For example, a person with birthday of  1964/09/25.
> How to compute his age with c#?
smilly - 10 May 2005 18:48 GMT
{sample}
private void button1_Click_1(object sender, System.EventArgs e)
        {
            DateTime d1=DateTime.Now;
            DateTime d2=DateTime.Now;
            try
            {
                d2=Convert.ToDateTime(textBox1.Text);
            }
            catch (Exception){}
            MessageBox.Show(
                "Your age is "+(d1.Year-d2.Year).ToString()
            );
        }

> Look at the DateTime.Subtract method, which will subtract two dates,
> returning a TimeSpan,
[quoted text clipped - 5 lines]
> > For example, a person with birthday of  1964/09/25.
> > How to compute his age with c#?
smilly - 10 May 2005 19:14 GMT
sorry about the previous post ;-)

using System;

namespace CommonLib
{
    public class DateRoutines
    {
        #region Methods

        public static int GetAge(DateTime aBirthDate)
        {
            TimeSpan span=DateTime.Now.Subtract(aBirthDate);
            return span.Days/365;
        }

        #endregion Methods
    }
}

> {sample}
> private void button1_Click_1(object sender, System.EventArgs e)
[quoted text clipped - 20 lines]
> > > For example, a person with birthday of  1964/09/25.
> > > How to compute his age with c#?
John Pick - 28 Mar 2008 23:45 GMT
int age =
   DateTime.Now.Year - DateOfBirth.Year +
   (DateTime.Now.DayOfYear < DateOfBirth.DayOfYear ? -1 : 0);

// e.g., if date of birth is Juy 4, 1976:

DateTime DateOfBirth = new DateTime(1976, 7, 4);
int age =
   DateTime.Now.Year - DateOfBirth.Year +
   (DateTime.Now.DayOfYear < DateOfBirth.DayOfYear ? -1 : 0);

From http://www.developmentnow.com/g/36_2005_5_0_0_517919/How-to-compute-age.ht
John Pick - 29 Mar 2008 08:10 GMT
Notice a bug in my code

From http://www.developmentnow.com/g/36_2005_5_0_0_517919/How-to-compute-age.ht
Steve Gerrard - 29 Mar 2008 16:29 GMT
> Notice a bug in my code?
>
[quoted text clipped - 3 lines]
> Posted via DevelopmentNow.com Groups
> http://www.developmentnow.com

Two words: leap year. ;-)

how about:
DateTime DateOfBirth = new DateTime(1976, 7, 4);
DateTime ThisDay = DateTime.Now.Date;
DateTime Birthday = new DateTime(ThisDay.Year, DateOfBirth.Month,
DateOfBirth.Day);
int age =
   ThisDay.Year - DateOfBirth.Year + (ThisDay < Birthday? -1 : 0);
Family Tree Mike - 29 Mar 2008 16:38 GMT
If your timing was great, the two different uses of DateTime.Now could be on
different dates.  Only problematic I believe, if the second one is the
birthday date.

> Notice a bug in my code?
>
> From http://www.developmentnow.com/g/36_2005_5_0_0_517919/How-to-compute-age.htm
>
> Posted via DevelopmentNow.com Groups
> http://www.developmentnow.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.