Results 1 to 6 of 6

Thread: Calculate Exact Age

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Calculate Exact Age

    I need to calculate the age in Years,Months and Days for which i am using the following code, but it give inappropriate result

    vb Code:
    1. Dim d As Date
    2.  
    3. If Date.TryParse(Me.DOB.Text, d) Then
    4.   Dim ts As TimeSpan = Today - d
    5.   Dim y As Double
    6.   Dim m As Double
    7.   Dim ds As Double = ts.TotalDays
    8.  
    9.   y = Math.Floor(ds / 365)
    10.   ds -= y * 365
    11.   m = Math.Floor(ds / 31)
    12.   ds -= m * 31
    13.  
    14.  
    15.   Me.TxtYears.Text = cstr(y)
    16.   Me.TxtMonths.Text=Cstr(m)
    17.   Me.TxtDays.Text = Cstr(ds)
    18. End If

    I also need to do the reverse calculation i.e get the DOB from the Age using the txtYears, txtMonths and TxtDays

    Plz Help Me

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Calculate Exact Age

    I dug this up. It does years and days

    Code:
        Public Function AgeCalc(ByVal DatePast As DateTime, _
                                    Optional ByVal DateFuture As DateTime = Nothing) As String
    
            'calculate age using two dates. the age will be years and days
            'USE:
            'Dim ageSTR() As String = AgeCalc(#2/29/1008#, #2/28/2009#).Split("|"c)
    
            'Debug.WriteLine(ageSTR(0)) 'years
            'Debug.WriteLine(ageSTR(1)) 'days
            'Debug.WriteLine(ageSTR(2)) 'how long calculation took (in ticks)
            '
            'note - the wonderful thing about DateSerial is that it takes care of 
            'Leap Birthdays AKA leaplings
            'a leaplings b-day is 3/1 for non-leap years
            Dim dp, df, nxtAnniv As DateTime, retval As String, ts As New TimeSpan
            Dim years, days As Integer, stpw As New Stopwatch
            stpw.Start() 'provide metric
            If DateFuture = Nothing Then DateFuture = DateTime.Now 'use now for future date
            If DatePast > DateFuture Then 'make sure the past is the past
                dp = DateFuture
                df = DatePast
            Else
                dp = DatePast
                df = DateFuture
            End If
            years = df.Year - dp.Year 'calculate years
            'create next "birthday"
            'nxtAnniv = New System.DateTime(df.Year, dp.Month, dp.Day) 'does not take care of leap b-day
            nxtAnniv = DateSerial(df.Year, dp.Month, dp.Day) 'takes care of leap b-day
            If nxtAnniv > df Then 'is the next "birthday" > future date
                years -= 1 'yes, adjust year
                nxtAnniv = DateSerial(df.Year - 1, dp.Month, dp.Day) 're-create so it is before future
            End If
            ts = df - nxtAnniv 'will give days
            days = ts.Days
            retval = years.ToString & "|" & days.ToString   'create return value
            stpw.Stop() 'how long did it take?
            retval &= "|" & stpw.ElapsedTicks.ToString
            Return retval
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Calculate Exact Age

    Thanks, but i need months also

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Calculate Exact Age

    Quote Originally Posted by aashish_9601 View Post
    Thanks, but i need months also
    What is a month?

    Try this modified version

    Code:
        Public Function AgeCalc(ByVal DatePast As DateTime, _
                                    Optional ByVal DateFuture As DateTime = Nothing, _
                                    Optional ByVal returnMosDay As Boolean = False) As String
    
            'calculate age using two dates. 
            'the age returned will be years and days or years months days
            'USE:
            'Dim ageSTR() As String = AgeCalc(#2/29/1008#, #2/28/2009#).Split("|"c)
    
            'Debug.WriteLine(ageSTR(0)) 'years
            'Debug.WriteLine(ageSTR(1)) 'days
            'Debug.WriteLine(ageSTR(2)) 'how long calculation took (in ticks)
            'or
            'Debug.WriteLine(ageSTR(0)) 'years
            'Debug.WriteLine(ageSTR(1)) 'months
            'Debug.WriteLine(ageSTR(2)) 'days
            'Debug.WriteLine(ageSTR(3)) 'how long calculation took (in ticks)
            '
    
            'note - the wonderful thing about DateSerial is that it takes care of 
            'Leap Birthdays AKA leaplings
            'a leaplings b-day is 3/1 for non-leap years
            Dim dp, df, nxtAnniv As DateTime, retval As String, ts As New TimeSpan
            Dim years, days As Integer, stpw As New Stopwatch
            stpw.Start() 'provide metric
            If DateFuture = Nothing Then DateFuture = DateTime.Now 'use now for future date
            If DatePast > DateFuture Then 'make sure the past is the past
                dp = DateFuture
                df = DatePast
            Else
                dp = DatePast
                df = DateFuture
            End If
            years = df.Year - dp.Year 'calculate years
            'create next "birthday"
            'nxtAnniv = New System.DateTime(df.Year, dp.Month, dp.Day) 'does not take care of leap b-day
            nxtAnniv = DateSerial(df.Year, dp.Month, dp.Day) 'takes care of leap b-day
            If nxtAnniv > df Then 'is the next "birthday" > future date
                years -= 1 'yes, adjust year
                nxtAnniv = DateSerial(df.Year - 1, dp.Month, dp.Day) 're-create so it is before future
            End If
            ts = df - nxtAnniv 'will give days
            days = ts.Days
            Dim mos As Integer = 0
            If returnMosDay Then 'return years mos days
                df = df.Date
                Dim na As DateTime = nxtAnniv.Date 'get anniversary date
                Dim la As DateTime = nxtAnniv.Date 'set trailing anniversary
                Do While na < df
                    na = na.AddMonths(1) 'add one month
                    If na < df Then 'less than future date
                        Dim td As Integer = (na - la).Days
                        If td <= days Then 'enough days?
                            days -= td 'subtract days
                            mos += 1 'increment monts
                            la = na 'set trailing anniversary
                        Else
                            Exit Do
                        End If
                    Else
                        Exit Do
                    End If
                Loop
                retval = years.ToString & "|" & mos.ToString & "|" & days.ToString   'create return value
            Else 'return years and days
                retval = years.ToString & "|" & days.ToString   'create return value
                stpw.Stop() 'how long did it take?
            End If
            retval &= "|" & stpw.ElapsedTicks.ToString
            Return retval
        End Function
    Last edited by dbasnett; Oct 8th, 2010 at 07:14 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Calculate Exact Age

    Use DateDiff.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Calculate Exact Age

    Quote Originally Posted by MarMan View Post
    Use DateDiff.
    In the code I provided I tried to preserve days also. If you just wanted months the code could have been simpler. Little about DateTime is straight forward.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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