Results 1 to 7 of 7

Thread: How old are you?

  1. #1
    Guest

    Post

    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.

  2. #2
    Lively Member
    Join Date
    Oct 1999
    Posts
    101

    Post

    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:

    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

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Post

    '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

  4. #4
    New Member
    Join Date
    Aug 1999
    Posts
    13

    Post

    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

  5. #5
    Guest

    Post

    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.

  6. #6
    Addicted Member Cbomb's Avatar
    Join Date
    Jul 1999
    Posts
    153

    Post

    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.

    Code:
    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)


  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Post

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width