For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > February 2005 > Age Calculation









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Age Calculation
Tommy Two Heads

2005-02-11, 4:03 pm

As a new commer to VB, I am having difficulty in writing code to calculate
someones' age form a date input from drop down pick lists in the form Year,
Month, Day. Can anyone please help?
Thanks.
Tommy Two Heads
Veign

2005-02-11, 4:03 pm

Scroll down to a post called 'Getting Age from Date of Birth' on 1/27

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--

"Tommy Two Heads" <TommyTwoHeads@discussions.microsoft.com> wrote in message
news:F5883660-1D76-4D41-9322-51297A94F073@microsoft.com...
> As a new commer to VB, I am having difficulty in writing code to calculate
> someones' age form a date input from drop down pick lists in the form

Year,
> Month, Day. Can anyone please help?
> Thanks.
> Tommy Two Heads



Bob Butler

2005-02-11, 4:03 pm

"Veign" <NOSPAMinveign@veign.com> wrote in message
news:e07aflFEFHA.1012@TK2MSFTNGP14.phx.gbl
> Scroll down to a post called 'Getting Age from Date of Birth' on 1/27


Isn't that highly dependent not only on what newsreader you use but how long
you keep articles and how you sort them? For me that thread is long gone
and scrolling down can't be done because there is no scrollbar! <g>

[url]http://groups.google.co.uk/groups?hl=en&lr=&frame=right&th=ab53d040c8fc7f5b&sm=uy%24C8t4CFHA.2288%40TK2MSFTNGP14.phx.gbl#link1[/url]

Rick Rothstein

2005-02-11, 4:03 pm

> As a new commer to VB, I am having difficulty in writing code to
calculate
> someones' age form a date input from drop down pick lists in the form

Year,
> Month, Day. Can anyone please help?


From a previous posting of mine. Note that the function calculates the
years, months and days between any two dates, so you need to provide it
with two dates as arguments. If you are calculating a person's current
age, then one of those arguments should be either the Now function or
the Date function (your choice). Also, the two dates may be input in any
order.

Rick - MVP

Usually, measuring differences between dates where months is one of the
measurement intervals is not considered "exact" since the same number of
total days difference will report differently depending on the months
spanned by the dates. The only exception I can think of is reporting how
long till someone's birthday. Anyway, I think it does what you want.
I've chosen to give you the "read out" from the function using the words
"Days", "Months" and "Years" (note that is properly singularizes and/or
pluralizes the text parts), but you can change that if you wish.

Also not that a single year is calculated by going from the month and
day of one year to the same month and day in the next year. A single
month is calculated by going from the day in one month to the same day
in the next month. This produces a "slight" problem when bridging from
a leap year day (February 29th) to a future date in a subsequent year
for a month greater than February. Consider for example Feb 29, 2000 to
Mar 1, 2001. Since their is no Feb 29th in 2001, what is one year from
Feb 29th to be? The routine I posted assumes one year from Feb 29th of a
leap year takes you to Mar 1st of the next year. This is only a problem
when the starting date is Feb 29th and is not a concern for any other
date combination.

Function YMD(StartDate As Date, EndDate As Date) As String
Dim TempDate As Date
Dim NumOfYears As Long
Dim NumOfMonths As Long
Dim NumOfWs As Long
Dim NumOfDays As Long
Dim NumOfHMS As Double
Dim TSerial1 As Double
Dim TSerial2 As Double
NumOfYears = DateDiff("yyyy", StartDate, EndDate)
TSerial1 = TimeSerial(Hour(StartDate), _
Minute(StartDate), Second(StartDate))
TSerial2 = TimeSerial(Hour(EndDate), _
Minute(EndDate), Second(EndDate))
NumOfHMS = 24 * (TSerial2 - TSerial1)
If NumOfHMS < 0 Then
NumOfHMS = NumOfHMS + 24
EndDate = DateAdd("d", -1, EndDate)
End If
StartDate = DateSerial(Year(EndDate), _
Month(StartDate), Day(StartDate))
If StartDate > EndDate Then
StartDate = DateAdd("yyyy", -1, StartDate)
NumOfYears = NumOfYears - 1
End If
NumOfMonths = DateDiff("m", StartDate, EndDate)
StartDate = DateSerial(Year(EndDate), _
Month(EndDate), Day(StartDate))
If StartDate > EndDate Then
StartDate = DateAdd("m", -1, StartDate)
NumOfMonths = NumOfMonths - 1
End If
NumOfDays = Abs(DateDiff("d", StartDate, EndDate))
YMD = CStr(NumOfYears) & " year" & _
IIf(NumOfYears = 1, "", "s")
YMD = YMD & ", "
YMD = YMD & CStr(NumOfMonths) & " month" & _
IIf(NumOfMonths = 1, "", "s")
YMD = YMD & ", "
YMD = YMD & CStr(NumOfDays) & " day" & _
IIf(NumOfDays = 1, "", "s")
End Function

Jeff Johnson [MVP: VB]

2005-02-11, 4:03 pm


"Bob Butler" <tiredofit@nospam.com> wrote in message
news:%23mfggoFEFHA.936@TK2MSFTNGP12.phx.gbl...

>
> Isn't that highly dependent not only on what newsreader you use but how
> long
> you keep articles and how you sort them? For me that thread is long gone
> and scrolling down can't be done because there is no scrollbar! <g>


And I would need to scroll UP!

I feel excluded based on the tone of your instructions, Chris. Whom do I
sue?


Veign

2005-02-11, 4:03 pm

Scroll UP <piff> <g>

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--

"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:evMUD2GEFHA.1408@TK2MSFTNGP10.phx.gbl...
>
> "Bob Butler" <tiredofit@nospam.com> wrote in message
> news:%23mfggoFEFHA.936@TK2MSFTNGP12.phx.gbl...
>
gone[color=darkred]
>
> And I would need to scroll UP!
>
> I feel excluded based on the tone of your instructions, Chris. Whom do I
> sue?
>
>



Wart

2005-02-14, 4:06 pm

TTH,
Just in case you can't follow the instructions from others ;-) try this.
Dim ldStartDate As Date
Dim ldEndDate As Date

ldStartDate = "12/25/1955"
ldEndDate = Now

'Year
Debug.Print DateDiff("yyyy", ldStartDate, ldEndDate)

'month
Debug.Print DateDiff("m", ldStartDate, ldEndDate)

'day
Debug.Print DateDiff("d", ldStartDate, ldEndDate)

You can replace the interal based on your pick list.
HTH,
CF


"Tommy Two Heads" <TommyTwoHeads@discussions.microsoft.com> wrote in message
news:F5883660-1D76-4D41-9322-51297A94F073@microsoft.com...
> As a new commer to VB, I am having difficulty in writing code to calculate
> someones' age form a date input from drop down pick lists in the form
> Year,
> Month, Day. Can anyone please help?
> Thanks.
> Tommy Two Heads



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com