Date math question

  • Thread starter Thread starter Woody Splawn
  • Start date Start date
W

Woody Splawn

I am trying to determine the age of a person based on two dates, the Date of
Birth and Today().

I have a function that does this but it is kludgey and is giving me an age
that is pretty close but only because I have figured into the equation
(manually) the approximate number of leap years.
The code reads:

Dim TS As New TimeSpan
Dim NumDays As Integer
Dim age As Double
Dim DaysInYear As Integer = 365
Dim LeapYearDays As Integer = 10

StartDate = #11/5/1946#
EndDate = #10/29/2003#

TS = EndDate.Subtract(StartDate)

NumDays = CInt(TS.TotalDays.ToString)
NumDays = NumDays - LeapYearDays

age = (NumDays / DaysInYear)
MsgBox("Number days: " + NumDays.ToString)
MsgBox("Age: " + age.ToString)

I would guess there is a better cleaner way of getting what I want. How do
I factor-in leap years without using a hard coded value like 10? In this
case I just sort guesstimated that there were two leap years for each 10
years of age.
 
Public Function currentAge(ByVal birthDay As Date) As Single
Return Math.Abs(DateDiff(DateInterval.Year, birthDay, Now))
End Function

hth,

steve
 
Hi Woody,

How do you define a year of age? In other words when does the year go up
by one?

Putting it another way. If my birthday falls on November 5th, and today is
less than Nov 5th, do I get this year or am I in the fractional part? On Nov
5th itself, do I then get the year? How about Nov 6th?

So the real question is - does the number of days matter at all in
determining the number of years?

Having got the number of years (by the method hinted at above), the number
of days between last birthday and today is pretty easy. If you want it as a
fraction, divide by 366 only if the Feb between the birthday and today is a
leap month.

Regards,
Fergus
 
i knew someone was gunna ask that...probably why i subconciously made the
original function return a single. ;^)

Public Function currentAge(ByVal birthDay As Date) As Single
Dim years As Integer = Math.Abs(DateDiff(DateInterval.Year, birthDay, Now))
Dim days As Integer = Math.Abs(birthDay.DayOfYear - Now.DayOfYear)
Return years + Math.Round(days / (366 - CInt(Not
Date.IsLeapYear(Now.Year))), 2)
End Function

massage the math.round parameter (2) for the "sensitivity" you desire (the
point at which you consider bumping the year up by one).

hth,

steve


| Hi Woody,
|
| How do you define a year of age? In other words when does the year go
up
| by one?
|
| Putting it another way. If my birthday falls on November 5th, and
today is
| less than Nov 5th, do I get this year or am I in the fractional part? On
Nov
| 5th itself, do I then get the year? How about Nov 6th?
|
| So the real question is - does the number of days matter at all in
| determining the number of years?
|
| Having got the number of years (by the method hinted at above), the
number
| of days between last birthday and today is pretty easy. If you want it as
a
| fraction, divide by 366 only if the Feb between the birthday and today is
a
| leap month.
|
| Regards,
| Fergus
|
|
 
Thanks for you help.

I had to adjust the formula some because I have option strict on. The
formula is giving me a result that is incorrect. My code is as follows:

Dim dblYears As Double = Math.Abs(DateDiff(DateInterval.Year,
StartDate, Now))
Dim days As Integer = Math.Abs(StartDate.DayOfYear - Now.DayOfYear)
dblAge = dblYears + Math.Round(days / (366 - CInt(Not
Date.IsLeapYear(Now.Year))), 2)
iAge = CInt(Fix(dblAge))
Return iAge

I get a result of 57. This is incorrect. The answer should be 56. It
should not be 57 for another 7 days.

Any thoughts?

I made up my own little formula. It seems to give me accurate data.

TS = EndDate.Subtract(StartDate)

NumDays = CInt(TS.TotalDays.ToString)
LeapYearDays = CInt(Fix(NumDays) / 365)
'There are approximately 2 leapyeardays for each 10 years
'Meaning each 10 years of age should have about 2 days less.
LeapYearDays = CInt(Fix(LeapYearDays) / 10)
LeapYearDays = CInt(LeapYearDays * 2)

NumDays = CInt(NumDays - LeapYearDays)

dblAge = NumDays / DaysInYear
'Fix removes the fractional part of the number
'And returns the remaining integer value
iAge = CInt(Fix(dblAge))

'MsgBox("Number days: " + NumDays.ToString)
'MsgBox("Age: " + age.ToString)
Return iAge

P.S. I don't want to make a federal case out of this. If it is accurate
within a few days over 50 years that's good enough.
 
This should work...

Public Function currentAge(ByVal birthDay As Date) As Single
Dim bdayofyear, ndayofyear As Integer
bdayofyear = birthDay.DayOfYear
ndayofyear = Now.DayOfYear
If Date.IsLeapYear(birthDay.Year) And bdayofyear > 60 Then bdayofyear -= 1
If Date.IsLeapYear(Now.Year) And ndayofyear > 60 Then ndayofyear -= 1
Return Now.Year - birthDay.Year + CInt((bdayofyear > ndayofyear))
End Function
 
Woody Splawn said:
I made up my own little formula. It seems to give me accurate data.
'There are approximately 2 leapyeardays for each 10 years
P.S. I don't want to make a federal case out of this. If it is accurate
within a few days over 50 years that's good enough.

Woody... consider just being "accurate" rather than "within a few days" :-)
It's a formula like calculating the area of a rectangle, there is no need to
be "close" when being correct is as easy as using the right formula. It's
2003... we can't still be disputing how to calculate somebody's age can we?

Given a variable "d" as a date and "age" as a long (and if you just want
the age in years) try

age = DateDiff(DateInterval.Year, d, Now.Date) _
- CLng(IIf(Format(d, "MMdd") > Format(Now.Date, "MMdd"), 1, 0))
 
Hi Woody,

YAAC - Yet Another Age Calculator.

Public Function currentAge (DoB As Date) As Integer
Dim NumYears As Integer = CInt (DateDiff (DateInterval.Year, DoB, Now))
DoB = New Date (Now.Year, Dob.Month, Dob.Day)
Return CInt (IIf (Dob > Now, NumYears - 1, NumYears))
End Function

Or if you prefer it a bit longer:

Public Function currentAge (DoB As Date) As Integer
Dim NumYears As Integer = CInt (DateDiff (DateInterval.Year, DoB, Now))
If Dob.Month > Now.Month Or _
(Dob.Month = Now.Month And Dob.Day > Now.Day) Then
Return NumYears - 1
Else
Return NumYears
End If
End Function

Regards,
Fergus
 
Hi Tom,

Yep. That first one brings the total function length up from 4 lines to a
whopping 5!! That's 20% extra!! Lol, and the second one spreads out all over
the place!

What I was after, though, was a numers-only solution. Dates to formatted
strings just to do a comparison?? Anyone would think you're programming in a
high-level language with rich libraries!

;-))

I wonder whether we'll get any more versions.

Regards,
Fergus
 
Back
Top