|
-
Jun 8th, 2006, 01:26 PM
#1
Thread Starter
Fanatic Member
finding someone's age
Greetings,
If someone's date of birth is March 5, 1982, and I need to find out how old they were as of February 28, 2006, what fucntion could I use.
When I use
client_age:datediff("yyyy", [DOB], "2/28/06") ,
the age result is a year bigger than it should be.
Tried a couple other differnt ways; but it doesn't like the syntax. Please help!
Thanks!
Jim
-
Jun 8th, 2006, 01:32 PM
#2
Addicted Member
Re: finding someone's age
If all you are interested in the number of years ("I am 29...again") then try:
VB Code:
'<CSCM>
'---------------------------------------------------------------------
'-- Name: GetPersonsAge
'-- By: <SGD> on 04/27/2006
'-- Desc: Returns the exact age of a person in years based on birthday, and
'-- an optional second date (default value is NOW)
'---------------------------------------------------------------------
'-- Note: DateDiff does not work properly for this.
'---------------------------------------------------------------------
'-- Revd: <SGD> - 05/26/2006
'---------------------------------------------------------------------
'-- Parm: BirthDate (Variant)
' LaterDate (Variant)
'---------------------------------------------------------------------
'</CSCM>
Public Function GetPersonsAge(BirthDate As Variant, _
Optional LaterDate As Variant) As Long
Dim dteLaterDate As Date
If IsDate(BirthDate) Then
If Not (IsDate(LaterDate)) Then
dteLaterDate = CVDate(Now)
Else
dteLaterDate = LaterDate
End If
'---------------------------------------------------------------------
GetPersonsAge = Year(dteLaterDate) - Year(BirthDate)
'---------------------------------------------------------------------
'-- Subtract one if this year's birthday hasn't occurred yet
'---------------------------------------------------------------------
If DateSerial(Year(dteLaterDate), Month(BirthDate), Day(BirthDate)) > dteLaterDate Then
GetPersonsAge = GetPersonsAge - 1
End If
End If
End Function
'*********************************************************************
'-- EXAMPLE CODE
'*********************************************************************
'-- Assume that today is 5/8/2006
'Debug.Print GetPersonsAge("05/31/1950") '-- Prints 55 which is correct
'Debug.Print DateDiff("yyyy", "05/31/1950", "05/08/2006") '-- Prints 56 which is incorrect
'Debug.Print GetPersonsAge("05/32/1950") '-- Prints 0 which is correct
-
Jun 8th, 2006, 01:36 PM
#3
Re: finding someone's age
 Originally Posted by JimMuglia
Greetings,
If someone's date of birth is March 5, 1982, and I need to find out how old they were as of February 28, 2006, what fucntion could I use.
When I use
client_age:datediff("yyyy", [DOB], "2/28/06") ,
the age result is a year bigger than it should be.
Tried a couple other differnt ways; but it doesn't like the syntax. Please help!
Thanks!
Jim
are you sure??
i tried this:
VB Code:
DateDiff("yyyy", "3/5/1982", "2/28/2006")
and it worked fine. showed 24
-
Jun 8th, 2006, 01:39 PM
#4
Re: finding someone's age
?? datediff("yyyy", #3/5/1982#, #2/28/2006#) = 24 which is correct
change your 2/28 to a date... and try cdate([dob])
VB Code:
datediff("yyyy", CDate([DOB]), #2/28/2006#)
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 01:41 PM
#5
Addicted Member
Re: finding someone's age
...I must be missing something: If I was born in 1982 but my birthday in 2006 has NOT come yet, then I am 23, not 24....???
-
Jun 8th, 2006, 01:56 PM
#6
Re: finding someone's age
if your birthday has NOT come up yet.. true.. BUT
if u are comparing it to 2/28/2006 then yes.. u are 24
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 01:59 PM
#7
Re: finding someone's age
 Originally Posted by sigid
...I must be missing something: If I was born in 1982 but my birthday in 2006 has NOT come yet, then I am 23, not 24....??? 
in that case, try sigid example.
-
Jun 8th, 2006, 02:01 PM
#8
Re: finding someone's age
oops.. its rounding up!!
d = DateDiff("d", #8/22/1986#, Date)
MsgBox d / 365
19.8
DateDiff("yyyy", #8/22/1986#, Date)
20
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 02:03 PM
#9
Addicted Member
Re: finding someone's age
Static -
- I agree with everything you said....
If I was born on March 5, 1982 and today is 2/28/2006 (not yet March 5, 2006) then...23...
-
Jun 8th, 2006, 02:09 PM
#10
Re: finding someone's age
heres a one liner! LOL
VB Code:
Left(CStr(DateDiff("d", #8/22/1986#, Date) / 365), InStr(CStr(DateDiff("d", #8/22/1986#, Date) / 365), ".") - 1)
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 02:12 PM
#11
Addicted Member
Re: finding someone's age
this one is much better:
VB Code:
Dim theage As String
theage = InputBox("How old are you?")
lol...  
Are you with me folks!
-
Jun 8th, 2006, 02:15 PM
#12
Re: finding someone's age
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 02:17 PM
#13
Addicted Member
Re: finding someone's age
Try this one out for size:
? Left(CStr(DateDiff("d", #6/9/1986#, Date) / 365), InStr(CStr(DateDiff("d", #6/9/1986#, Date) / 365), ".") - 1)
Since today is 6/8/06, and the above yields 20, instead of 19... 
(also yields 20 for 6/10/1986, 6/11/1986...why am I such a nitpicker???)
-
Jun 8th, 2006, 02:20 PM
#14
Re: finding someone's age
oops.. leap years..
Left(CStr(DateDiff("d", #6/9/1986#, Date) / 365.25), InStr(CStr(DateDiff("d", #6/9/1986#, Date) / 365.25), ".") - 1)
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 02:22 PM
#15
Re: finding someone's age
Blah.. ok.. so it wasnt perfect.. If u put todays date it to breaks oh well.. gave it a shot LOL
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 02:31 PM
#16
Re: finding someone's age
Wait!! one more shot.. I think I got it!
One line 
oh.. taking input from text1 ....
VB Code:
Left(CStr(DateDiff("d", CDate(Text1), Date) / 365.25), _
IIf(InStr(CStr(DateDiff("d", CDate(Text1), Date) / 365.25), ".") <> 0, _
InStr(CStr(DateDiff("d", CDate(Text1), Date) / 365.25), ".") - 1, _
Len(CStr(DateDiff("d", CDate(Text1), Date) / 365.25))))
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 02:43 PM
#17
Addicted Member
Re: finding someone's age
Sorry, but when you hardcode 365.25, the starting date becomes critical.
Try the above for 6/8/1988 and you'll get 17...
(and some time real soon now, I am definitely going to get a real life...)
-
Jun 8th, 2006, 02:53 PM
#18
Re: finding someone's age
bah...
17.9986310746064
so close! thats it.. I give up...
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 03:00 PM
#19
Re: finding someone's age
(Don't forget the International Date Line)
-
Jun 8th, 2006, 03:09 PM
#20
Re: finding someone's age
i haven't tested it, but try this on for size:
VB Code:
Dim lAge As Long
lAge = DateDiff("yyyy", #8/22/1986#, Now)
If CDate(Format$(Now, "mm/dd")) < CDate(Format$(#8/22/1986#, "mm/dd")) Then lAge = lAge - 1
Debug.Print lAge
-
Jun 8th, 2006, 03:35 PM
#21
Thread Starter
Fanatic Member
Re: finding someone's age
I should have posted this in the database section, as I need to put it for a value in the Access query wizard. I tried the left command above, and its giving me an "invalid procedure call".
Jim
-
Jun 8th, 2006, 03:41 PM
#22
Re: finding someone's age
OOOHH in access.. ok
use Sigid's code (towards the top)
drop the function into a module then use it in the field
AGE:GetPersonsAge([DOB],#2/28/2006#)
should work fine
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 03:47 PM
#23
Re: finding someone's age
here's mine in functional form:
VB Code:
Private Function GetAge(ByVal dDOB As Date, ByVal dDate As sdate) As Long
GetAge = DateDiff("yyyy", dDOB, dDate)
If CDate(Format$(dDate, "mm/dd")) < CDate(Format$(dDOB, "mm/dd")) Then GetAge = GetAge - 1
End Function
that'll work fine in VBA (i think)
-
Jun 8th, 2006, 03:50 PM
#24
Re: finding someone's age
yes it will
although be forewarned.. it will slow down your query to call functions like that
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 8th, 2006, 09:37 PM
#25
Re: finding someone's age
how bout this?
VB Code:
Dim dtBirth As Date, dtNow As Date, dtVar As Date
Dim lngAge As Long
dtNow = Date
dtBirth = "6/10/2000"
lngAge = DateDiff("yyyy", dtBirth, dtNow)
dtVar = DateAdd("YYYY", lngAge, dtBirth)
If dtVar > dtNow Then
lngAge = lngAge - 1
End If
MsgBox lngAge
-
Jun 12th, 2006, 09:33 AM
#26
Re: finding someone's age
I know this thread is a couple of days old now, but i thought of a better way to do it:
VB Code:
Private Function GetAge(ByVal dDOB As Date) As Long
GetAge = Year(Now - dDOB + 2) - 1900
End Function
Last edited by bushmobile; Jun 12th, 2006 at 11:27 AM.
-
Jun 12th, 2006, 11:16 AM
#27
Re: finding someone's age
On the person's birthday it should advance a year. IOW, if you were born on 6/12/2000, you're 6 today. So the algorithm should be GetAge = Year(Now - dDOB + 1) - 1900, or it'll be off by one day. (Unless age is figured differently in the UK?)
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 12th, 2006, 11:19 AM
#28
Re: finding someone's age
i didn't really test it - you're probably right. code above ammended.
Edit: I think it needs to be +2 actually.
Last edited by bushmobile; Jun 12th, 2006 at 11:26 AM.
-
Jun 12th, 2006, 12:23 PM
#29
Re: finding someone's age
Wow - I cannot believe this thread...
Calc'ing AGE is a standard formula - not really open for debate!
But not on this forum 
[edit] I thought this would blow up in VB with Feb 29...
dtVar = DateAdd("YYYY", lngAge, dtBirth)
but it only blows up with DATEADD in T-SQL
Last edited by szlamany; Jun 12th, 2006 at 12:27 PM.
-
Jun 12th, 2006, 02:50 PM
#30
Re: finding someone's age
 Originally Posted by bushmobile
i didn't really test it - you're probably right. code above ammended.
Edit: I think it needs to be +2 actually.
For a birthday of tomorrow (mine, actually), 1 makes me a year older from tomorrow on. 2 makes me a year older today.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 12th, 2006, 06:13 PM
#31
Re: finding someone's age
well for the example you gave it needs to be +2. ahh well, s*d it!
-
Jun 13th, 2006, 12:38 AM
#32
Fanatic Member
Re: finding someone's age
-
Jun 13th, 2006, 07:00 AM
#33
Re: finding someone's age
Doing division and math tricks to determine an age is a dangerous path to take. It might look neat - but it's simply not a proper formula for age.
Calculating an age has to be one of the simplest logic-flow concepts to understand. It has one PROCESS step and one CONDITION step...
1) PROCESS: Subtract Birth year from Current Year
2) CONDITION: Is Current MM/DD greater than Birth MM/DD
3) NO: Subtract 1
4) YES: Do nothing
Result...Age
You can do this without using DATEDIFF and DATEADD - it's the easiest of math.
It works in VB - it works in SQL - it works in any language.
It handles leap years - not through any effort, but simply because the logic flow mimics the real life calculation that one would do in their own head to determine their own age.
No one is dividing by 365.25 in their head
-
Jun 13th, 2006, 07:03 AM
#34
Re: finding someone's age
no-one is saying that doesn't work, szlamany - it was the very first reply. i think people are just trying to think of different ways.
-
Jun 13th, 2006, 07:14 AM
#35
Re: finding someone's age
My concern is that those other techniques are ridden with possibilies for bugs - division, rounding, variable datatypes (float/integer).
I'll keep quiet now
-
Jun 12th, 2007, 01:33 AM
#36
Re: finding someone's age
But how the get axact today age in year Month Day format.
-
Jun 12th, 2007, 02:15 AM
#37
Re: finding someone's age
 Originally Posted by shakti5385
But how the get axact today age in year Month Day format.
what do you mean?
and if anyone's bothered to read down this far, here's the age calculation without an if:
Code:
Private Function GetAge(ByVal DOB As Date) As Long
GetAge = DateDiff("yyyy", DOB, Date) + (Date < DateSerial(Year(Date), Month(DOB), Day(DOB)))
End Function
-
Jun 14th, 2007, 11:01 AM
#38
Addicted Member
Re: finding someone's age
I just found this (again!) and I like bushmobile's formula better than my own...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|