Results 1 to 6 of 6

Thread: If you need to calculate someone's age...

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    12

    Smile If you need to calculate someone's age...

    ...Here is how you do it. (VB.NET)
    VB Code:
    1. Public Function CalculateAge(Birth As DateTime, TodayDate As DateTime) As String
    2.  
    3.         Dim dob As DateTime
    4.         dob = Birth
    5.         Dim tday As TimeSpan = Birth.Subtract(TodayDate)
    6.         Dim years As Integer, months As Integer, days As Integer
    7.  
    8.         months = 12 * (TodayDate.Year - dob.Year) + (TodayDate.Month - dob.Month)
    9.  
    10.         If TodayDate.Day < dob.Day Then
    11.             months -= 1
    12.             days = DateTime.DaysInMonth(dob.Year, dob.Month) - dob.Day + TodayDate.Day
    13.         Else
    14.             days = TodayDate.Day - dob.Day
    15.         End If
    16.  
    17.         years = Math.Floor(months / 12)
    18.         months -= years * 12
    19.  
    20.         Return years & "y " & months & "m " & days & "d"
    21.  
    22.     End Function
    Last edited by FunkyDexter; Apr 28th, 2016 at 03:34 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: If you need to calculate someone's age...

    There's a place for posting examples - vb.net codebank

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    12

    Re: If you need to calculate someone's age...

    I suspected something like this.

    Going there. I thought the routine might have been useful for someone.

    :-)

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: If you need to calculate someone's age...

    No worries, I moved it for you.

    If you're going to post code you should also be aware of our code tags function. It's the VB or # button. The first will allow you to enter the language you're using and will produce a nicely formatted and coloured output. The second produces a more basic "catch all" output.

    Finally, welcome to the forums and thanks for the contribution
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    12

    Re: If you need to calculate someone's age...

    :-)

    I noticed the "code" tag too late, and posted in the wrong place.
    Soooo... I think that as a new entry I did good enough, right?

    Hello to everybody!

  6. #6
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: If you need to calculate someone's age...

    Well most people's first post is to ask a question rather than provide some help so your definitely holding a positive balance as far as karma concerned
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

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