3

I am new to progress and I want to calculate age from the date of birth and I have no idea how to do it. If anyone knows about this please help me. Thanks in advance.

What I have tried so far is:

define var dob as date.
define var age as character.
assign
      dob = 09/16/1988.
      age = STRING(INT(YEAR(TODAY) - YEAR(dob ))).
      message age view-as alert-box.

it show age 30 but in real the age is 29.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Ajay Jangra
  • 174
  • 1
  • 13

3 Answers3

8

Use the interval function.

define var dob as date initial 09/16/1988.

message interval( today, dob, "years" ) view-as alert-box.

Returns 29 (as long as today is before the 16th of this year) - handles leap years fine too.

Stefan Drissen
  • 3,266
  • 1
  • 13
  • 21
3

Something you can do in ABL is to subtract dates. which gives the age in days. You could then divide that by 365 and round it down to give the age in years. Obviously this doesn't take leap years into account, so isn't 100% accurate.

DEFINE VARIABLE dob AS date.
DEFINE VARIABLE age AS INTEGER.
ASSIGN
      dob = 09/16/1988
      age = TRUNCATE(((TODAY - 03/25/1979) / 365),0).
      MESSAGE age VIEW-AS ALERT-BOX.

Alternatively, if you want it to be accurate including leap years you have to work a bit harder. What I've done here is to add the number of years to the birth date to see if the birthday has already happened. If it hasn't then I take 1 year away. There's probably more elegant solutions, but it will do the job!

DEFINE VARIABLE dob AS date.
DEFINE VARIABLE age AS INTEGER.
DEFINE VARIABLE comp AS DATE NO-UNDO.
ASSIGN
      dob = 09/16/1988.
comp = add-interval(dob,year(TODAY) - year(dob),"YEARS").
IF TODAY GT comp THEN 
    age = year(TODAY) - year(dob).
ELSE 
    age = year(TODAY) - year(dob) - 1.           
MESSAGE age VIEW-AS ALERT-BOX.
jdpjamesp
  • 762
  • 7
  • 19
3

In your code you are just count years, that is not the correct way. It will be shown just difference of the year. You need to count years based on month and days also. So, you can try the following code that will work also for the leap year.

    define var dob as date.
    define var vYears as int.
    define var age as int.

    assign 
         dob = 09/16/1988.
    vYears = int(year(today) - year(dob)).

    if (month(today) < month(dob)) then do:
    age = vYears - 1.
    end.
    if (month(today) = month(dob)) then do:
      if (day(today) < day(dob)) then do:
           age = vYears - 1.
      end.
      else do:
           age = vYears.
     end.
    end.
    if (month(today) > month(dob)) then do:
         age = vYears.
    end.

message age view-as alert-box.
jdpjamesp
  • 762
  • 7
  • 19