Thanks:  0
Likes:  0
Dislikes:  0

# Thread: function to calculate date of birth

1. ## function to calculate date of birth

Hi,
I need to write a function that will take today's date (GetDateTime()) and ensure that a person is say 27 years old given his date of birth. So I need to write a function that will calculate his date of birth to ensure this age is adhered to.
Any suggestions? I was gonna covert todays to a string and then do some equation from here? Any ideas welcome... thanks

2. ## Re: function to calculate date of birth

Get number of days between current date and birth date. Then divide by 365 and result should be integer.
Otherwise, Count difference between years, months,days and do verification..

3. ## Re: function to calculate date of birth

If current year - birth year &gt;= 27 and current month/day &gt; birth month/day....

- Dave

4. ## Re: function to calculate date of birth

Thanks guys,

I used the function below in the end...

string Getdate()
string sDateOfBirth
return sDateOfBirth

where FormatDate is a function that formats the date and 3830 is roughly (365 * 27) plus 6 months in days also...

6. ## Re: function to calculate date of birth

The TimeDate functions will account for leap years for you.

7. ## Re: function to calculate date of birth

Dualtha,
It seems like your function is limited to testing for age 27 only.

The function below works with any age. Just supply the person's date of birth and the age you want to test for.

Note: The supplied date of birth must be of the format: yyyy-mm-dd. This just makes it simpler for the logic to cast the date string into a DATETIME value. You could make it accept a different format by calling FormatDateTime.

I hope this is helpful for you.

- Jim

</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">BOOLEAN IsOfAge(STRING sBirthDate, INTEGER iTargetAge)
//************************************************** ************************************************** **
// Description: Returns TRUE if the specified target age has been reached as of today.
//
// Logic: We add the specified # of years (target age) to the given birth date.
// If that new date is greater than or equal to the target age, then
// the person is at least that old.
//
// Parameters:
// STRING sDateOfBirth - The date of birth in question. Format must be: yyyy-mm-dd
//
// INTEGER iTargetAge - The age (in years) that you're testing for.
//
// Return: TRUE if the person (specified by the given birthdate) has reached
// the specified age, else FALSE.
//************************************************** **************************************************
STRING sToday, sYearOfBirth, sYearOfBirthPlus, sBirthDatePlus
DATETIME dtToday, dtYearOfBirthPlus
INTEGER iYearOfBirth, iDaysDiff

sToday = FormatDateTime (GetDateTime(), &quot;yyyy-mm-dd&quot // get today's date (with no TIME portion)

sYearOfBirth = GetField(sBirthDate,&quot;-&quot;,1) // get birth year from supplied date

iYearOfBirth = Val(sYearOfBirth) // convert birth year to integer

sYearOfBirthPlus = Str(iYearOfBirth + iTargetAge) // add target years to birth year

sBirthDatePlus = StrTran(sBirthDate,sYearOfBirth,sYearOfBirthPlus) // create date to compare VS today

iDaysDiff = DiffDateTime ([DATETIME]sToday, [DATETIME]sBirthDatePlus) // compare dates

return (iDaysDiff&gt;=0) // return TRUE if person is at least target age</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">

8. ## Re: function to calculate date of birth

oops - I just noticed a mistake in the function header comments. The correct text is:
</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">// Logic: We add the specified # of years (target age) to the given birth date.
// If that new date is greater than or equal to today's date, then
// the person is at least that old.</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.