473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Date math question

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.
Nov 20 '05 #1
12 1654
Public Function currentAge(ByVal birthDay As Date) As Single
Return Math.Abs(DateDiff(DateInterval.Year, birthDay, Now))
End Function

hth,

steve
Nov 20 '05 #2
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
Nov 20 '05 #3
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
"Fergus Cooney" <fi*****@post.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
| 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
|
|
Nov 20 '05 #4
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.

Nov 20 '05 #5
What date are you passing in to get a result of 57?

"Woody Splawn" <wo***@splawns.com> wrote in message
news:uA**************@tk2msftngp13.phx.gbl...
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.

Nov 20 '05 #6
The same as before 11/05/1946 and Today(()

Nov 20 '05 #7
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" <wo***@splawns.com> wrote in message
news:OB*************@TK2MSFTNGP11.phx.gbl...
The same as before 11/05/1946 and Today(()

Nov 20 '05 #8
"Woody Splawn" <wo***@splawns.com> wrote...
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))

Nov 20 '05 #9
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
Nov 20 '05 #10
"Fergus Cooney" <fi*****@post.com> wrote...
YAAC - Yet Another Age Calculator.
Or if you prefer it a bit longer:


Hi Fergus,

They look like longer versions of the one-liner I posted :-)
Nov 20 '05 #11
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
Nov 20 '05 #12
> age = DateDiff(DateInterval.Year, d, Now.Date) _
- CLng(IIf(Format(d, "MMdd") > Format(Now.Date, "MMdd"), 1, 0))


Thank you. That was very helpful.


Nov 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex | last post by:
I have two SQL statements: Statement #1 SELECT CARRIER_REFERENCE_NUMBER, CURRENT_FLEET_SIZE, trunc(Fleet_Size_Effective_From_Date), trunc(Fleet_Size_Effective_To_Date) FROM...
1
by: Robert Mark Bram | last post by:
Howdy All! I am trying to write a very brief comparison of the Date and Math objects in terms of instance v static objects. What I have below is my best so far. Any criticisms or suggestions are...
2
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
6
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By "common" I mean at the least ones described in this...
3
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the...
1
by: Sam | last post by:
How do I convert Julian Date to Calendar Date in ASP.Net 1.1 based on following guideline found at Internet? To convert Julian date to Gregorian date: double JD = 2299160.5; double Z =...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
1
by: Mtek | last post by:
Hi, We have a form where the user selects a date from a calendar, the date is in the format May 23, 2008. The date in the datebase is in the format 05212008. What we need to do is get the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.