Solved

Calculate current age in years and months

Posted on 2013-05-22
5
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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