Click to See Complete Forum and Search --> : Age calculation (attempt 2)
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.
Gimpster
Jan 5th, 2000, 07:36 AM
There was a post like this recently. Look here (http://www.vb-world.net/ubb/Forum1/HTML/012630.html) for the answer to your question. Also, check out this (http://www.vb-world.net/ubb/Forum1/HTML/012635.html) post.
------------------
Ryan
jpark
Jan 5th, 2000, 07:42 AM
Input must include "/". ex) "3/22/70"
otherwise, use MaskEdBox control to mask it.
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
Lyla
Jan 11th, 2000, 09:11 PM
Hi.
jpark, It didn't really work for me.
Let's try this one.
A form with textbox , a label and a button:
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
cfmoxey
Jan 12th, 2000, 04:25 AM
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 cfmoxey@xre.com.
--Carl
jpark
Jan 12th, 2000, 05:15 AM
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).]
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.