473,657 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Subtrac t(StartDate)

NumDays = CInt(TS.TotalDa ys.ToString)
NumDays = NumDays - LeapYearDays

age = (NumDays / DaysInYear)
MsgBox("Number days: " + NumDays.ToStrin g)
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 1681
Public Function currentAge(ByVa l birthDay As Date) As Single
Return Math.Abs(DateDi ff(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(ByVa l birthDay As Date) As Single
Dim years As Integer = Math.Abs(DateDi ff(DateInterval .Year, birthDay, Now))
Dim days As Integer = Math.Abs(birthD ay.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 "sensitivit y" you desire (the
point at which you consider bumping the year up by one).

hth,

steve
"Fergus Cooney" <fi*****@post.c om> wrote in message
news:%2******** ********@TK2MSF TNGP11.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(DateDi ff(DateInterval .Year,
StartDate, Now))
Dim days As Integer = Math.Abs(StartD ate.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.Subtrac t(StartDate)

NumDays = CInt(TS.TotalDa ys.ToString)
LeapYearDays = CInt(Fix(NumDay s) / 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(LeapYe arDays) / 10)
LeapYearDays = CInt(LeapYearDa ys * 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.ToStrin g)
'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******** ******@tk2msftn gp13.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(DateDi ff(DateInterval .Year,
StartDate, Now))
Dim days As Integer = Math.Abs(StartD ate.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.Subtrac t(StartDate)

NumDays = CInt(TS.TotalDa ys.ToString)
LeapYearDays = CInt(Fix(NumDay s) / 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(LeapYe arDays) / 10)
LeapYearDays = CInt(LeapYearDa ys * 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.ToStrin g)
'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(ByVa l birthDay As Date) As Single
Dim bdayofyear, ndayofyear As Integer
bdayofyear = birthDay.DayOfY ear
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((bdayofyea r > ndayofyear))
End Function

"Woody Splawn" <wo***@splawns. com> wrote in message
news:OB******** *****@TK2MSFTNG P11.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(DateIn terval.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.Y ear, 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.Y ear, 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

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

Similar topics

1
8334
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 CAPS.CARRIER_FLEET_SIZE_HISTORY WHERE CARRIER_REFERENCE_NUMBER = 481
1
2073
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 most welcome! Date is an instance object. You use Date by creating instances of it - you call methods on those instances. - you change data to do with each instance.
2
10203
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 document.write(mmddyy)
4
5364
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 calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
6
4829
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 W3C note: http://www.w3.org/TR/NOTE-datetime For my requirements the code would be need to be open sourceable under the BSD license.
3
29034
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 week . I've used the weekday() function to convert dates to numberic days of the week (1-7) I've used cdbl (date) to convert a date to a serial number, but then I do math with the number and I can't seem to convert this back to a date.
1
4538
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 = Math.Floor(JD+0.5); double W = Math.Floor((Z - 1867216.25)/36524.25); double X = Math.Floor(W/4);
4
15725
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 which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e....
1
1539
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 starting and ending date of the week for the date selected in the form: May 23, 2008, and then get the records from the database where the dates fall within that range.
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.