Results 1 to 3 of 3

Thread: Compute present age

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Resolved Compute present age

    Timspan returns only the number of days (as the highest value). Maybe there's another way for returning the Year? Not to mention DateDiff function from Microsoft.VisualBasic namespace? Any function from the standard .Net base classes?

    Thanks.
    Last edited by nebulom; Jan 4th, 2006 at 01:22 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compute present age

    Given the fact that different months and years contain different numbers of days, paired with the fact that a TimeSpan has no specific start or end point with reference to any specific date and time, it is not possible for a TimeSpan to report a number of months or years. If you want a person's age in years then do it like this:
    Code:
                DateTime dob; // Date of birth.
                DateTime currentDate = DateTime.Today;
    
                int years = currentDate.Year - dob.Year;
    
                if (currentDate.Month < dob.Month)
                    --years;
    or something like:
    Code:
    int year = DateTime.Today.Month < dob.Month ? dob.Year - DateTime.Today.Year - 1 : dob.Year - DateTime.Today.Year;
    Last edited by jmcilhinney; Jan 4th, 2006 at 12:54 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Compute present age

    Ok thanks a million.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width