# Thread: Age in yrs and months

1. ## Age in yrs and months

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

Cur_Month=month(Date_Now)
DOB_Month=month(DOB)
If Cur_month>=DOB_Month
else
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)

'**(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)

'**(2B)below is where both dates are in the same month & cur day =day(dob)

'**(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)

2. ## RE: Age in yrs and months

Raymond,

Try this function; Note that Age= is one long expression line. Make sure it is all together or you should use a line continuation.

function Age as N(Birthdate as D,CompareDate as D)

' Computes difference in years of CompareDate-Birthdate
' If CompareDate is blank, it uses current date
' Value can return negative ages (which is to the future)

' Age({01/01/1999},{})

IF CompareDate={}
date1=DATE()
ELSE
date1=CompareDate
END IF

Age=YEAR(date1)-
YEAR(Birthdate)+IF(RIGHT(CDATE(Birthdate),4)>RIGHT(CDATE(date1),4),

end function

Regards,

Ira J. Perlow
Computer Systems Design & Associates
csda@mediaone.net

3. ## RE: Age in yrs and months

Thanks Ira,

I can see that your function results in a decimal. I thought about going that route and then turning the decimal into months. But I really wanted a function that would return years and months without any further converting through calculated fields. But of course there are lots of ways to skin this cat.

Ray

4. ## RE: Age in yrs and months

Raymond, how do you mean to define the age in 'months'?

If the current system date is June 1.
And birthdate is May 31 of the current year.

Is the child one month old? Or do you want the age to be zero months until 30 days have passed (except for February)?

-- tom

5. ## RE: Age in yrs and months

>>Or do you want the age to be zero months until 30 days have passed
(except for February)?

6. ## RE: Age in yrs and months

this function, elapsed_time(), gives the difference between 2 dates in years, months and days:

function elapsed_time as C(date1 as D,date2 as D)
dim y1 as n
dim y2 as n
dim m1 as n
dim days as n
dim d1 as d

if date1>date2 then
' swap the dates
dim tempd as d
tempd=date2
date2=date1
date1=tempd
end if
' first approximation to year difference

y1=year(date2)-year(date1)
if makedate(month(date1),day(date1),year(date2))>date2 then
y1=y1-1
m1=month(date2)-month(date1)+12
else
m1=month(date2)-month(date1)
end if

if m1+month(date1)>12 then
y2=y1+1
else
y2=y1
end if
if makedate(mod(month(date1)+m1,12),day(date1),year(date1)+y2)>date2 then
' day is too far
m1=m1-1
end if

d1=makedate(mod(month(date1)+m1,12),day(date1),year(date1)+y2)
days=date2-d1

elapsed_time=alltrim(str(y1))+" years and "+
alltrim(str(m1))+" months and "+alltrim(str(days))+" days"

end function

7. ## RE: Age in yrs and months

Peter,

I think you need to include the makedate function, unless it is something new a beta you are using. In any case it won't work as is in build 230.

Ray

8. ## RE: Age in yrs and months

Peter e-mailed the makedate function to me. Here it is, in case anyone is interested. I'm not sure what, if anything I'll put in the code archive on all this.

Ray

function makedate as D(m as N,d as N,y as N)
' when give month, day and year parameters, returns the date or the first
' preceding date that is a valid date
' e.g., makedate(2,29,97) returns {2/28/97}

if m>12 .or. m

9. ## RE: Age in yrs and months

Ray,

Here is a much easier function to return the results you want;

function AgeMonths as N(Date1 as D,CompareDate as D)

' Computes difference in months of CompareDate-Date1
' If CompareDate is blank, it uses current date
' Value can return negative ages (which is to the future)

' Examples of usage
' AgeMonths({01/01/1999},{})
' AgeMonths({01/01/1999},{06/10/1999})

' To compute Age in Years, just divide the result by 12, as in
' AgeMonths({01/01/1999},{})/12

AgeMonths=0

IF CompareDate={}
date2=DATE()
ELSE
date2=CompareDate
END IF

IF Day(Date2)>=Day(Date1)
AgeMonths=(YEAR(Date2)*12)+MONTH(Date2)-(YEAR(date1)*12)-Month(date1)+ ((Day(Date2)-Day(Date1))/31)
ELSE
' Last month from Date2

' First day of last Month
LstMo1st=LstMonth-DAY(LstMonth)+1

' Last day of previous month from date2
LstMoLst=Date2-DAY(date2)

AgeMonths=(YEAR(Date2)*12)+MONTH(Date2)-(YEAR(date1)*12)-Month(date1)-1+ (Date2-(LstMo1st-1+Min(DAY(LstMoLst),DAY(Date1))))/31
END IF

end function

Regards,

Ira J. Perlow
Computer Systems Design & Associates
csda@mediaone.net

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•