Pages

Tuesday 8 January 2013

Calculate Age

Public Shared Function AgeCalFun(ByVal Name As String, ByVal DOB As String) As String
        Dim YearDOB, YearCurrent, MonthDOB, MonthCurrent, DayDOB, DayCurrent, YearCal, MonthCal, DayCal As Integer
        Dim Result As String

        If IsDate(DOB) Then
            YearDOB = Year(DOB)
            MonthDOB = Month(DOB)
            DayDOB = Day(DOB)

            YearCurrent = Year(Now)
            MonthCurrent = Month(Now)
            DayCurrent = Day(Now)

            If DayCurrent < DayDOB Then
                DayCurrent = DayCurrent + 30
                MonthCurrent = MonthCurrent - 1
            End If

            If MonthCurrent < MonthDOB Then
                MonthCurrent = MonthCurrent + 12
                YearCurrent = YearCurrent - 1
            End If

            DayCal = DayCurrent - DayDOB
            MonthCal = MonthCurrent - MonthDOB
            YearCal = YearCurrent - YearDOB

            Result = "<b>" & Name & "</b> your age is <b>" & _
                     Format(YearCal, "00") & "</b> year(s), <b>" & _
                     Format(MonthCal, "00") & "</b> month(s) and <b>" & _
                     Format(DayCal, "00") & "</b> day(s)"

        Else
            Result = "<font color=red>Incorrect Date.</font>"
        End If

        Return Result
    End Function

No comments:

Post a Comment