Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# age with fractions
Message
 
 
À
07/06/2013 14:36:45
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01575875
Message ID:
01575880
Vues:
20
What is DateDiff here and does it calculate with decimals or just the integer age?


>>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
>
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform