Many age functions have been posted here that calculate an age as of the system date for a given birth date. The following is a good one, probably based on something someone else posted here in the past:
function Age as N(DOB as D)
'**Note: blank b-days will =this yr, i.e., 2000 in 2000
'**Future b-days will have negative ages
'**Ages 1 month to 11 months will be 0 cuz only doing years here
select
case month(date())>month(dob)
age=year(date())-year(dob)
case month(date())=month(dob).and.day(date())>=day(dob)
age=year(date())-year(dob)
case else
age=year(date())-year(dob)-1
end select
end function
However, I recently had need to determine a person's age in years and months, including for babies who are less than a year old. I also needed to allow the user to substitute a future date for the usual system date so that they could easily determine how old someone would be at some date in the future.
I present the following function here. I hope those more clever than I am can, in replies, improve upon it in terms of efficiency of elegance (maybe there is a wholly different and better way to do it). When the best shows up here I will post it in the code archive section.
Please improve upon it or find errors.
Raymond Lyons
function AgeSTR as C(DOB as D,Date_Now as D)
'**Note that if one wants just yrs as a numerical value
'** val(calc->result string from AgeSTR) gives the age in yrs with blanks being 0
dim ageSTR0 as c
dim Cur_Month as n
dim DOB_Month as n
dim Months_to_add_sub as n
Cur_Month=month(Date_Now)
DOB_Month=month(DOB)
If Cur_month>=DOB_Month
Months_to_add_sub=-(DOB_Month-Cur_Month)
else
Months_to_add_sub=12-(DOB_Month-Cur_Month)
End If
select
case DOB = {}
ageSTR0="Blank DOB"
case Date_Now = {}
ageSTR0= "Current Date is blank"
case DOB > Date_Now
ageSTR0="Invalid DOB"
'**(1A) below is for cur mon is greater than mon of DB
'**and cur day is greater than or = day of BD
case month(date_Now)>month(dob).and.day(Date_Now)>=day(dob)
ageSTR0=Ltrim(STR(year(Date_Now)-year(dob)))+" yrs "+Ltrim(STR(Months_to_add_sub))+" months"
'**(1B) below is for cur mon is greater than mon of DB
'**and cur day is less than day of BD
case month(Date_Now)>month(dob).and.day(Date_Now)= day of BD
case month(Date_Now)=month(dob).and.day(Date_Now)>=day(dob)
ageSTR0=Ltrim(STR(year(Date_Now)-year(dob)))+" yrs "+Ltrim(STR(Months_to_add_sub))+" months"
'**(2B)below is where both dates are in the same month & cur day =day(dob)
ageSTR0=Ltrim(STR(year(Date_Now)-year(dob)-1))+" yrs "+Ltrim(STR(Months_to_add_sub))+" months"
'**(3B) below is for cur mon is less than mon of DB
'**and cur day is less than day of BD (subtract 1 yr & subtract 1 month)
case month(Date_Now)
function Age as N(DOB as D)
'**Note: blank b-days will =this yr, i.e., 2000 in 2000
'**Future b-days will have negative ages
'**Ages 1 month to 11 months will be 0 cuz only doing years here
select
case month(date())>month(dob)
age=year(date())-year(dob)
case month(date())=month(dob).and.day(date())>=day(dob)
age=year(date())-year(dob)
case else
age=year(date())-year(dob)-1
end select
end function
However, I recently had need to determine a person's age in years and months, including for babies who are less than a year old. I also needed to allow the user to substitute a future date for the usual system date so that they could easily determine how old someone would be at some date in the future.
I present the following function here. I hope those more clever than I am can, in replies, improve upon it in terms of efficiency of elegance (maybe there is a wholly different and better way to do it). When the best shows up here I will post it in the code archive section.
Please improve upon it or find errors.
Raymond Lyons
function AgeSTR as C(DOB as D,Date_Now as D)
'**Note that if one wants just yrs as a numerical value
'** val(calc->result string from AgeSTR) gives the age in yrs with blanks being 0
dim ageSTR0 as c
dim Cur_Month as n
dim DOB_Month as n
dim Months_to_add_sub as n
Cur_Month=month(Date_Now)
DOB_Month=month(DOB)
If Cur_month>=DOB_Month
Months_to_add_sub=-(DOB_Month-Cur_Month)
else
Months_to_add_sub=12-(DOB_Month-Cur_Month)
End If
select
case DOB = {}
ageSTR0="Blank DOB"
case Date_Now = {}
ageSTR0= "Current Date is blank"
case DOB > Date_Now
ageSTR0="Invalid DOB"
'**(1A) below is for cur mon is greater than mon of DB
'**and cur day is greater than or = day of BD
case month(date_Now)>month(dob).and.day(Date_Now)>=day(dob)
ageSTR0=Ltrim(STR(year(Date_Now)-year(dob)))+" yrs "+Ltrim(STR(Months_to_add_sub))+" months"
'**(1B) below is for cur mon is greater than mon of DB
'**and cur day is less than day of BD
case month(Date_Now)>month(dob).and.day(Date_Now)= day of BD
case month(Date_Now)=month(dob).and.day(Date_Now)>=day(dob)
ageSTR0=Ltrim(STR(year(Date_Now)-year(dob)))+" yrs "+Ltrim(STR(Months_to_add_sub))+" months"
'**(2B)below is where both dates are in the same month & cur day =day(dob)
ageSTR0=Ltrim(STR(year(Date_Now)-year(dob)-1))+" yrs "+Ltrim(STR(Months_to_add_sub))+" months"
'**(3B) below is for cur mon is less than mon of DB
'**and cur day is less than day of BD (subtract 1 yr & subtract 1 month)
case month(Date_Now)
Comment