Solved

Calculate current age in years and months

Posted on 2013-05-22
5
359 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now