Solved

Calculate current age in years and months

Posted on 2013-05-22
5
394 Views
Last Modified: 2013-05-22
I have the following code that I had found which finds the persons current age from their date of birth.  The only problem is that it only shows the age in years.  I also need the months.  What can I add to also show the months?

    Protected Function formatAge(ByVal DOB As DateTime) As String

        Dim now As DateTime = DateTime.Now
        DOB = Convert.ToDateTime(ClientDOB.Text)
        Dim age As Integer = now.Year - DOB.Year
        If now < DOB.AddYears(age) Then
            age -= 1
        End If

        formatAge = age.ToString()

    End Function

Open in new window

0
Comment
Question by:huerita37
5 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 39189868
Timespan is what you need, you can go right down to milliseconds!

http://msdn.microsoft.com/en-us/library/system.timespan.aspx
0
 
LVL 4

Accepted Solution

by:
mohammad827 earned 500 total points
ID: 39189873
DateDiff(DateInterval.Month, Now, DOB)
Above should give you difference in month.
Divide it by 12 and you will get year and month
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 39189892
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 39189893
Timespan gives you all of that without any division or further math's being required.
0
 

Author Closing Comment

by:huerita37
ID: 39189917
I was able to find the correct answer with using DateInterval.

Here is what I did:

 Protected Function formatAge(ByVal DOB As DateTime) As String

        Dim dt1, dt2 As Date
        dt1 = DOB
        dt2 = now
        Dim Months As Long
        Dim Diff As Long = DateDiff(DateInterval.Month, dt1, dt2)
        Dim Years As Long = Diff \ 12
        Months = Diff - Years * 12
        formatAge = Years & " y  " & Months & " m "

    End Function
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question