Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calculate the age
Message
From
02/06/2019 00:29:15
 
 
To
02/06/2019 00:25:07
General information
Forum:
Visual FoxPro
Category:
Visual FoxPro and .NET
Miscellaneous
Thread ID:
01668878
Message ID:
01668879
Views:
89
Likes (1)
>how is the faster way to calculate the age in VFP?
>
>eg.
>born: 02-05-2000
>today: 02-06-2019
>result: 19 years 1 month
>
>Thanks in advance

This should be easily convertable to VFP:
    ' Calculate age
    Public Function CalcAge() As Boolean
        Dim ldBirth As Date = Date.Now
        Dim ldBirthAdjusted As Date = Date.Now
        Dim ldTarget As Date = Nothing
        Dim ldTemp As Date = Date.Now
        Dim lnDrop As Integer = 0

        ' Reset the values
        nDay = 0
        nMonth = 0
        nYear = 0

        ' Work with a local variable
        ldBirthAdjusted = dBirth
        ldTarget = dTarget

        ' If the birth date is greater than the target date
        If dBirth > ldTarget Then
            ldTemp = ldTarget
            ldTarget = ldBirthAdjusted
            ldBirthAdjusted = ldTemp
        End If

        Try
            ldBirth = New Date(ldTarget.Year, ldBirthAdjusted.Month, ldBirthAdjusted.Day)
        Catch loException As Exception

            ' If the date cannot be calculated, this is because we have a leap year
            ldBirth = New Date(ldTarget.Year, 3, 1)

            ' If we have a leap year
            If ldTarget.Month > 2 Then
                lnDrop = 1
            End If

        End Try

        ' If the birth date is greater than the target date
        If ldBirth > ldTarget Then
            nYear = ldTarget.Year - ldBirthAdjusted.Year - 1
        Else
            nYear = ldTarget.Year - ldBirthAdjusted.Year
        End If

        ' If the birth date day is greater than the target day
        If ldBirthAdjusted.Day > ldTarget.Day Then
            nMonth = (ldTarget.Month - ldBirthAdjusted.Month + 12 - 1) Mod 12
        Else
            nMonth = (ldTarget.Month - ldBirthAdjusted.Month + 12) Mod 12
        End If

        ldTemp = New Date(ldBirthAdjusted.Year + nYear, ldBirthAdjusted.Month, ldBirthAdjusted.Day)

        nDay = Math.Abs(DateDiff(DateInterval.Day, ldTarget, ldTemp.AddMonths(nMonth))) - lnDrop

        Return True
    End Function
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Reply
Map
View

Click here to load this message in the networking platform