[AccessD] Date Diff Help It cant be this hard

Gustav Brock Gustav at cactus.dk
Mon Jan 9 11:24:27 CST 2006


Hi Arthur

I love the word "reliably" ...
Try this:

? Int(DateDiff("d", #1/9/1955#, Date())/365.26)
? Int(DateDiff("d", #1/9/1956#, Date())/365.26)

It would be nice but there is no simple and foolproof way.
If age calculation is for birthday greeting cards to the boys in the football club, who cares (well, I could think of some, but the World would still stand), but if you need an age for an insurance case it has to be right. Not to forget that more than 3 mio. people are born on the 29th. February ...

/gustav


>>> artful at rogers.com 09-01-2006 17:28:20 >>>
Here is a solution for you. You have stumbled upon a couple of croquet hoops
in date calculations, but here is a solution for you. First, using my
birthday as the value (rather than your control name), look at these
results:
? datediff("yyyy", #11/15/1947#, Date())
 59 
Which is wrong: I am 58 not 59: the problem is that the above examines only
the year.
? datediff("d", #11/15/1947#, Date())
 21240 
Which correctly returns the number of days between my birthdate and today.
? 21240 / 365.26
 58.1503586486339
This divides the number of days by the number of days in a year (the .26 is
for leap years and handles them correctly).
Now just Int() that value and you get the correct number of years. So,
ungainly as it is, the formula becomes:
? Int(DateDiff("d", #11/15/1947#, Date())/365.26)
There may be a simpler and foolproof way, but until someone presents it, you
can reliably use this. I would suggest creating a function AgeInYears() into
which you pass the DateOfBirth. Then when someone presents a simpler way to
do it, you can just change the content of the function.
HTH,
Arthur





More information about the AccessD mailing list