如何从 OpenEdge ABL 中的出生日期计算年龄?

How to calculate age from date of birth in OpenEdge ABL?

我是新手,我想从出生日期计算年龄,但我不知道该怎么做。如果有人知道这件事,请帮助我。 提前致谢。

到目前为止我尝试过的是:

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.

显示30岁实际29岁

您可以在 ABL 中做的事情是减去日期。它给出了以天为单位的年龄。然后,您可以将其除以 365,然后四舍五入得到以年为单位的年龄。显然这没有考虑闰年,所以不是 100% 准确。

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.

或者,如果您希望它准确,包括闰年,您必须更加努力。 我在这里所做的是将年数添加到出生日期以查看生日是否已经发生。如果还没有,那么我需要 1 年的时间。可能有更优雅的解决方案,但它会完成这项工作!

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.

在您的代码中,您只是计算年数,这不是正确的方法。它将只显示年份的差异。您还需要根据月份和日期来计算年份。所以,你可以试试下面的代码,它也适用于闰年。

    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.

使用interval函数。

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

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

Returns 29(只要今天在今年 16 号之前)- 也可以很好地处理闰年。