Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# age with fractions
Message
From
07/06/2013 14:36:45
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01575875
Message ID:
01575878
Views:
35
>I am wondering if someone already has a method calculating age with fractions? I started to code our current VFP code, but don't want to re-invent the wheel if it's already known problem.

This is my CalcAge class in .NET. Maybe you can find something useful in there.
Namespace Framework

    Public Class CalcAge

        Public dBirth As Date = Nothing
        Public dTarget As Date = Nothing
        Public nDay As Integer = 0
        Public nMonth As Integer = 0
        Public nYear As Integer = 0
        Private oApp As Framework.App = Nothing
        Private oProcess As Framework.LXProcess = Nothing

        ' This is when we access the class in a desktop mode
        Public Sub New(ByVal toApplication As Framework.App)
            oApp = toApplication
        End Sub

        ' This is when we access the class in a Web or Web Service mode
        Public Sub New(ByVal toProcess As Framework.LXProcess)
            oProcess = toProcess
            oApp = oProcess.oApp
        End Sub

        ' 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 loError 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

    End Class

End Namespace
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
Next
Reply
Map
View

Click here to load this message in the networking platform