CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Calculating Age

  1. #1
    Join Date
    Mar 2001
    Location
    Missouri
    Posts
    1

    Calculating Age

    This has been re-posted from another list, so sorry if you're seeing it twice.

    I am having trouble with DateTime and TimeSpan classes. I need a function
    that calculates the number of Years (integral value) between two dates.
    The TimeSpan looked promising, but alas the largest unit returned is days.

    Int32 AgeInYears( DateTime startDate, DateTime endDate )
    {
    /* Help, ugh! */
    }

    Just to be safe, targetDate1.Year - startDate.Year is not sufficient as it
    is not the difference in between the nominal years I am interested in. Rather, I want to get
    at the number of years in the span between the target and the start. Also,
    just using the TimeSpan.Day and assuming that 365 days = 1 year does not work
    either as leap years would be ignored, etc.


    TIA!
    Aaron


  2. #2
    Join Date
    Apr 2001
    Location
    Bucks, UK
    Posts
    10

    Re: Calculating Age

    This should help you. Calculates the age from today, needs a small change for your requirement
    DateTime dTodaysDate = DateTime.Today;
    int nToday = dTodaysDate.Year * 10000 + dTodaysDate.Month * 100 + dTodaysDate.Day;
    int nBirth = m_dBirthDate.Year * 10000 + m_dBirthDate.Month * 100 + m_dBirthDate.Day;
    int nAge = (nToday - nBirth) / 10000;



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

Featured