We do it like these routines: -
/********************************************************************
**
** File: date2age.p
**
** Author: Chris Paulson
**
** Date: 14-mar-95
**
** Calculates a age from a date
**
** Mods:-
** CJP - 13-Sep-95
** Reserve space for translating text
**
** M.Hills - 31/01/2000
** Changed "\" to "/" as causing problems with UNIX.
*/
define input param dt as date no-undo.
define var i as int no-undo.
i = today - dt.
run general/agetext.p( i ).
return return-value.
/* end proc */
/************************************************
**
** File: agetext.p
**
** Author: Chris Paulson
**
** Date: 14-jul-94
**
** Display an age in nice format
**
** Mods:-
** CJP - 13-Sep-95
** Reserve space for translating text
** Surjit - Added 0.5 if reminder is greater than 0.9
*/
define input param d as integer no-undo.
define var t as char format "x(60)":u no-undo.
define var vdiff as decimal no-undo.
if d >= 365.25 then do:
vdiff = d / 365.25.
vdiff = vdiff - integer(vdiff - 0.5).
if vdiff > 0.9 then
d = d + 0.5.
t = string( integer(truncate(d / 365.25,0))) + "Y":T4.
end.
else if d >= 169 then
t = string( integer(d / 30.4375)) + "M":T4.
else if d >= 30.4375 then
t = string( integer(d / 7.024038461538)) + "W":T4.
else
t = string( d ) + "D":T4.
return t.
/* end proc */
/********************************************************************
**
** File: calcage.p
**
** Author: Chris Paulson
**
** Date: 14-jul-94
**
** Calculates a inputed age into the number of days
**
** Mods:-
** CJP - 13-Jul-95
** Reserve space for translating text
*/
define input param age-inp as character no-undo.
define input param defUnit as char no-undo.
define output param age-int as integer no-undo.
define var tDec as decimal no-undo.
if (age-inp = "") or (age-inp = ?) then do:
age-int = ?.
return.
end.
if index(age-inp, "M":T4) > 0 then do:
age-int = decimal( substring(age-inp, 1, index(age-inp, "M":T4) - 1 ) ) * 30.4375 no-error.
end.
else if index(age-inp, "W":T4) > 0 then do:
age-int = decimal( substring(age-inp, 1, index(age-inp, "W":T4) - 1 ) ) * 7.024038461538 no-error.
end.
else if index(age-inp, "D":T4) > 0 then do:
age-int = decimal( substring(age-inp, 1, index(age-inp, "D":T4) - 1 ) ) no-error.
end.
else if index(age-inp, "Y":T4) > 0 then do:
assign tDec = decimal( substring(age-inp, 1, index(age-inp, "Y":T4) - 1 ) ) no-error.
if tDec > 0 then
age-int = 1 + tDec * 365.25 no-error.
else do:
if tDec < 0 then
assign age-int = -1. /* Any negative value in order to cause a bad return */
else
assign age-int = 0.
end.
end.
else do:
/*********CDW mod 7/1/97 ***********/
if defUnit = "Y":T4 then
age-int = 1 + decimal( age-inp ) * 365.25 no-error.
else if defUnit = "M":T4 then
age-int = decimal( age-inp ) * 30.4375 no-error.
else if defUnit = "W":T4 then
age-int = decimal( age-inp ) * 7.024038461538 no-error.
else if defUnit = "D":T4 then
age-int = decimal( age-inp ) no-error.
end.
if error-status:error or age-int < 0 then do:
message "Invalid format":25T skip "Please re-enter":20T skip.
return "bad":u.
end.
return "good":u.
/* end proc */