Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 questions on how to figure out a Date Calculation 1

Status
Not open for further replies.

eelsar

Technical User
May 20, 2002
36
US
First Question:
How to I figure out the date difference between two dates, for example 7/8/01 and 8/8/02?

Second Question:
How do I figure out a persons age based on their date of birth, and that it will constantly update itself as the date changes?

Thank you!
 
DateDiff("d",dte1,dte2) time interval can be changed to weeks, months, years. Set variable dte2 = Date() and the date diff will change with the current system date.
 
Code:
Public Function basAge(BirthDay As Date) As Long

    'Michael Red 8/8/02     Simplistic Age Cals

    basAge = DateDiff("yyyy", BirthDay, Date)
    basAge = basAge + (DateSerial(Year(Date), Month(BirthDay), Day(BirthDay)) > Date)

End Function
[code]

 MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Use NJLDoc's code. Sorry MichaelRed but your code will give useless results.

quoting you "There is never time to do it right but there is always time to do it over"

Do it over MichaelRed and TEST before posting next time.
 
SemperFiDownUnda,

I accept the critique, but would like to know the specifics of your "complaint". Many (most) of the routines for 'age' calculation will fail in certain certain circumstances, however the results of my (really really elaborate) testing:


? basAge(#8/7/42#)
60

? basAge(#8/8/42#)
60

? basAge(#8/9/42#)
59


which do not appear, to me, to be all that outrageous.




MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Before I got these replies I figured out how to calculate Age

Private Sub maskDateOfBirthChild_LostFocus()
'to figure out a childs age
txtAgeChild.Text = DateDiff("yyyy", maskDateOfBirthChild.Text, (Format(Now, "Short Date")))
End Sub

My question is will Now update the same as Date()?

Thank you!
 
Whoops, I see how you where using it. The question wasn't very specific. But its still a clunky method IMHO, but my opinion doesn't matter much.

eelsar you your code works but it would be faster to do this

Private Sub maskDateOfBirthChild_LostFocus()
'to figure out a childs age
txtAgeChild.Text = DateDiff("yyyy", maskDateOfBirthChild.Text, Now())
End Sub

grabbing the formating the date to short date just causes vb to convert that string back to a date....kind of like going

i = 5 + 6

compaired to
i = CINT(CSTR(5)) + CINT(CSTR(6))

just not needed.
 
eelsar: That will not work that way.

If my birthday is tomorrow, then your datediff function will return the same age for me as today!

See MichaelRed's suggestion (and give him a star for answering your question correctly) *******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
I tried MichaelReed's code and it worked.

basAge = DateDiff("yyyy", BirthDay, Date)
basAge = basAge + (DateSerial(Year(Date), Month(BirthDay), Day(BirthDay)) > Date)

I tried with my code testing if my birthday were tomorrow and the answer was inaccurate.

When I tried the above code - it worked very accurately.

Thank you!

 

Ok. I'll give him the star myself..... *******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
CC,

(if I may be so bold as to adress you in the familiar)

Thank you,

Kudos and offers of gainful employment are accepted with appreciation.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top