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 / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Is this a good way to get an age from a date of birth?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
needin4mation@gmail.com - 02 Aug 2005 22:35 GMT
Hi, this is what I did:

//this will work even though a decimal is returned because
//everyone will have a double digit age returned
//unless 9 and under or over 99 are in the database;
//can work to catch wrongly entered dates of birth;
//it is a given all ages must be two digits; there will
//never be otherwise in this scenario

    DateTime dtThen = new DateTime ();
    dtThen = Convert.ToDateTime(PerReader["dob"]);
    DateTime dtNow = DateTime.Now;
    TimeSpan ts = dtNow - dtThen;
    double age = (ts.TotalDays / 365);
    txtAge.Text = age.ToString().Substring(0,2);

Comments and improvements are welcome on how to get someone's age from
a date of birth in a database.  Thanks!
Brendan Green - 03 Aug 2005 05:01 GMT
Well, the first thing that I can see as an issue is that not every year has
365 days.

It looks to me that you're trying to get the whole number of years for their
age?

> Hi, this is what I did:
>
[quoted text clipped - 14 lines]
> Comments and improvements are welcome on how to get someone's age from
> a date of birth in a database.  Thanks!
needin4mation@gmail.com - 03 Aug 2005 13:52 GMT
Yeah.  Is there a better way to get an age?
Brendan Green - 04 Aug 2005 03:41 GMT
How about:

DateTime dob = new DateTime(1970, 01, 01);

DateTime now = DateTime.Now;

Console.WriteLine(now.Year - dob.Year);

> Yeah.  Is there a better way to get an age?

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.