PDA

Click to See Complete Forum and Search --> : How old are you?


Dec 30th, 1999, 04:28 AM
How do i calculate someone's age when you only input their date of birth. I think it has something to do with recursion but i don't know how to do this
Please someone help.

ChrisJackson
Dec 30th, 1999, 04:35 AM
Hi Marc.

Create a new VB project. Put a text box on it and a command button.

Go to the editor and paste this code:


Option Explicit

Private Sub Command1_Click()
Dim iAge As Integer
iAge = DateDiff("yyyy", CDate(Text1), Now)
MsgBox iAge
End Sub


Be sure that you put a valid date in the Text Box.

All the best.

Chris

HeSaidJoe
Dec 30th, 1999, 04:51 AM
'get a persons age if birthday is supplied

Option Explicit

Public Function GetAge(DateString As String) As Integer
Dim lDate As Date
lDate = Left$(DateString, 2) + "/" + Mid$(DateString, 3, 2) _
+ "/" + Right$(DateString, 4)
GetAge = DateDiff("yyyy", lDate, Now)
End Function

Private Sub Command1_Click()
'use your birthday in this format mmddyyyy
Dim x As String 'this is your birthday var
x = 12301970 'value
MsgBox GetAge(x) 'call function
End Sub

KnightM
Dec 30th, 1999, 09:54 AM
Don't forget to use quotes (") around the value for x in the above message, or any birthday from January-September will not work. (They have a 0 in the first digit place which is removed if the quotes are not used)

-KnightM

Dec 30th, 1999, 05:28 PM
Thats great thanks alot. My next question is how do you find out someones age in years, months and days e.g. 28 years 2 months and 10 days old.

Cbomb
Dec 31st, 1999, 12:31 AM
Here is the code i used "Bday" is what ever your birthday input is and it updates in real time. It may need some modification for your uses. Make sure there is a timer and a label on your form.


Option Explicit

Dim Bday As Date
Dim iSecs As Single
Dim Years As Long
Dim Days As Long
Dim Hours As Long
Dim Minutes As Long
Dim Seconds As Long

Private Sub Form_Load()
Bday = #12/16/85#
iSecs = DateDiff("s", Bday, Now)
Years = Fix((((iSecs / 60) / 60) / 24) / 365)
Days = Fix(((iSecs / 60) / 60) / 24) Mod 365
Hours = Fix((iSecs / 60) / 60) Mod 24
Minutes = (iSecs / 60) Mod 60
Seconds = iSecs Mod 60
Timer1_Timer
End Sub

Private Sub Timer1_Timer()
Label1.Caption = Years & " Years, " & Days & " Days, " & Hours & " Hours, " & Minutes & " Minutes " & Seconds & " Seconds"
Seconds = Seconds + 1
If Seconds = 60 Then
Minutes = Minutes + 1
Seconds = 0
End If

If Minutes = 60 Then
Hours = Hours + 1
Minutes = 0
End If

If Hours = 24 Then
Days = Days + 1
Hours = 0
End If

If Days = 365 Then
Years = Years + 1
Days = 0
End If
End Sub




------------------
Cbomb
Teen Programmer
Techie 8)

Frans C
Dec 31st, 1999, 09:50 AM
I'm sorry, but they all don't work. I'm not 35, but 34 and I'm born on 31 jan 1965 and it is now 1 jan 2000. I'm not fit enough to work it out right now (had a party before), but when I wake up in an hour or 10 I will work it out.