Sorry for the wait... I was quite busy lately.
I hope this produces some answers that are correct. I tested it a few times and it returned right ages for all my family members
Code:
Option Static
Option Explicit
Declare Function isValidDate (Year As Integer, Month As Integer, Day As Integer) As Byte
Declare Function isLeapYear (Year As Integer) As Byte
Declare Function getPersonAge (Year As Integer, Month As Integer, Day As Integer, _
NowYear As Integer, NowMonth As Integer, NowDay As Integer) As Integer
Dim Year As Integer, Month As Integer, Day As Integer
'@ Ask the user for input YYYYMMDD and check if it's valid
Dim Datum As String, isOk As Byte = Not( 0 ), i As Integer
Dim NumberStr As String
NumberStr = "0123456789"
Do
isOk = Not( 0 )
Input "Enter a valid birthday in format YYYYMMDD: ", Datum
If Len(Datum) <> 8 Then
isOk = 0
Else
For i = 0 To 7
If Instr(NumberStr, Mid$(Datum, i + 1, 1)) = 0 Then isOk = 0
Next i
If isOk Then
Year = Val(Left$(Datum, 4))
Month = Val(Mid$(Datum, 5, 2))
Day = Val(Right$(Datum, 2))
isOk = isValidDate(Year, Month, Day)
End If
End If
If Not( isOk ) Then Print "Invalid entry"
Loop Until isOk
'@ Get the current Date (format MM-DD-YYYY)
Dim CurrDate As String, CurrYear As Integer, CurrMonth As Integer, CurrDay As Integer
CurrDate = Date$
CurrYear = Val(Right$(CurrDate, 4))
CurrMonth = Val(Left$(CurrDate, 2))
CurrDay = Val(Mid$(CurrDate, 4, 2))
Print getPersonAge(Year, Month, Day, CurrYear, CurrMonth, CurrDay)
Sleep
End
Private Function getPersonAge (Year As Integer, Month As Integer, Day As Integer, _
NowYear As Integer, NowMonth As Integer, NowDay As Integer) As Integer
Dim printed As integer = 0
'@ Very simple loop to calculate the age
Dim lYear As Integer, lMonth As Integer, lDay As Integer, rAge As Integer = 0
For lYear = Year To NowYear
Dim MinMonth As Integer, MaxMonth As Integer
If lYear = Year Then MinMonth = Month Else MinMonth = 1
If lYear = NowYear Then MaxMonth = NowMonth Else MaxMonth = 12
For lMonth = MinMonth To MaxMonth
Dim MaxDay As Integer, MinDay As Integer
Select Case lMonth
Case 1, 3, 5, 7, 8, 10, 12 : MaxDay = 31
Case 4, 6, 9, 11: MaxDay = 30
Case 2: MaxDay = 28 + Abs(isLeapYear(lYear))
End Select
If lYear = Year And lMonth = Month Then MinDay = Day + 1 Else MinDay = 1
If lYear = NowYear And lMonth = NowMonth Then MaxDay = NowDay
For lDay = MinDay To MaxDay
If lMonth = Month And lDay = Day Then rAge += 1
Next lDay
Next lMonth
Next lYear
getPersonAge = rAge
End Function
Private Function isValidDate (Year As Integer, Month As Integer, Day As Integer) As Byte
'@ Checks if a year is a valid year
If Year < 0 Then isValidDate = 0: Exit Function
If Month < 1 Then isValidDate = 0: Exit Function
If Day < 1 Then isValidDate = 0: Exit Function
If Month > 12 Then isValidDate = 0: Exit Function
Dim MaxDay As Integer
Select Case Month
Case 1, 3, 5, 7, 8, 10, 12 : MaxDay = 31
Case 4, 6, 9, 11: MaxDay = 30
Case 2: MaxDay = 28 + Abs(isLeapYear(Year))
End Select
If Day > MaxDay Then isValidDate = 0: Exit Function
isValidDate = -1
End Function
Private Function isLeapYear (Year As Integer) As Byte
isLeapYear = ((Year MOD 4 = 0) AND (Year MOD 100 <> 0)) OR (Year MOD 400 = 0)
End Function
I hope it works and suits your challenge objectives.