I am working on an app where I have to calculate the age of a person. Inside of my age class I have a function that is designed to account for leap year. Is there anything I am missing or could improve upon? I would have though this would have been more complicated so I am not confident in my method or my test case.
The function:
Function calculateAge() As Integer
Dim age As Integer
Try
If Not Birthdate.Day.Equals(29) And Birthdate.Month.Equals(2) Then
age = todaysDate.Year - Birthdate.Year
Else
age = todaysDate.Year - Birthdate.Year - 1
End If
Return age
Catch ex As Exception
Console.WriteLine(ex.ToString)
Return Nothing
Finally
age = Nothing
End Try
End Function
The test:
Sub Main()
'Test calculate age function'
Console.WriteLine(chip.calculateAge)
'Test calculate age function with a February 29th birthday'
Dim steve As New clsAge("Steve")
With steve
.Birthdate = #2/29/2008#
End With
Dim date1 As DateTime = #2/29/2000#
Dim date2 As DateTime = #2/28/2009#
Dim age As Integer
Console.WriteLine(date1.Day.ToString)
If Not date1.Day.Equals(29) And date1.Month.Equals(2) Then
age = date2.Year - date1.Year
Console.WriteLine(age.ToString)
Else
age = date2.Year - date1.Year
age = age - 1
Console.WriteLine(age.ToString())
End If
Console.ReadLine()
End Sub
DateTime
is the same as VB'sDate
. \$\endgroup\$