Results 1 to 6 of 6

Thread: Age calculation (attempt 2)

  1. #1
    Guest

    Post

    By only inputing someones date of birth into VB how do I calculate there age in years, months and days e.g. 34 years 5 months and 14 days.
    I posted this question about a week ago and Frans C said he/she would answer it after he/she got rid of his/her hangover. He/she has either not been able to solve the problem or has one hell of a hangover.

  2. #2
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    There was a post like this recently. Look here for the answer to your question. Also, check out this post.

    ------------------
    Ryan

  3. #3
    Member
    Join Date
    Jan 1999
    Location
    Garden Grove, CA, Orange
    Posts
    55

    Post

    Input must include "/". ex) "3/22/70"
    otherwise, use MaskEdBox control to mask it.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim HowOld As String
    
       If KeyAscii = vbKeyReturn Then
          HowOld = Format(Date - CDate(Text1), "MMDDYY")
          Label1 = "You are " & Mid(HowOld, 5, 2) & " years " _
                   & Mid(HowOld, 1, 2) & " months and " _
                   & Mid(HowOld, 3, 2) & " days old"
       End If
    End Sub
    Joon


  4. #4
    Addicted Member
    Join Date
    Jul 1999
    Location
    Portland, OR.
    Posts
    226

    Post

    Hi.
    jpark, It didn't really work for me.
    Let's try this one.
    A form with textbox , a label and a button:
    Code:
    Function Agecal(myDate As Variant) As Integer
       myDate = CDate(Text1)
       Dim Totdays As Long
         Totdays = DateDiff("y", myDate, Date)                             '  Total Number of days
         Step1 = Abs(Totdays / 365.25)                                              '  Number of years
         Step2 = (Step1 - Int(Step1)) * 365.25 / 30.435                     '   Number of months
        Step3 = CInt((Step2 - Int(Step2)) * 30.435)                          '   Number of days
         If myDate < Date Then
               Label1 = "It has been:    " & Int(Step1) & "  Year(s)  " & Int(Step2) & "  Month(s)  " & Int(Step3) & "  Day(s)" & "  Since that date"
            Else
               Label1 = " There are:    " & Int(Step1) & "  Year(s)  " & Int(Step2) & "  Month(s)  " & Int(Step3) & "  Day(s)" & "  To this date"
         End If
    End Function
    
    
    Private Sub Command1_Click()
        If IsDate(Text1) = False Then
            MsgBox " Please enter a valid date. "
            Text1.SetFocus
            Text1.SelStart = 0:   Text1.SelLength = Len(Text1)
            Exit Sub
        End If
          Agecal (myDate)
    End Sub
    If there is a btter way, please let me know.

    Good Luck

    Lyla

  5. #5
    Member
    Join Date
    Jan 2000
    Location
    Southbridge, MA, USA
    Posts
    40

    Post

    One of the problems I discovered when coding an age calculator was when does one become a month older. I assumed that one became a month older on the same date or if the month changed in the interim. For example, if my birthdate is 30 October 1950, then I would be 50 years and one month on 30 November 2000. But if my birthdate were 31 October 1950, then on 30 November 2000, I would be 50 years and 29 days, and would not be 50 y, 1 m until 1 December 2000. If anyone is interested in the functions I created to calculate this all out (about 350 lines of code; it could be shortened, but it had already taken me a week to get all the bugs out them), please email me at [email protected].
    --Carl

  6. #6
    Member
    Join Date
    Jan 1999
    Location
    Garden Grove, CA, Orange
    Posts
    55

    Post

    Lyla, You are right.

    It dose not produce a correct answer.

    Mark, If u had already been used my code (I don't think you are, though), I'm sorry that I suggested you the incorrect code.

    Joon



    [This message has been edited by jpark (edited 01-12-2000).]

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