Issue with Calculating Age in DMN Using DMN JS

Description:

I’m working on a decision model using DMN JS to calculate the age based on a birthdate (dateDeNaissance) in a DMN table. However, I’m encountering a problem where the expression is returning null. I also tried using a literal expression, but it similarly returned null.

Error Details:

  • Expressions Used:
    • Literal Expression:
year(years and months duration(date(dateDeNaissance), now()))
  • Alternative Expression:
floor(days between(date(dateDeNaissance), now()) / 365)

Steps to Reproduce:

  1. Define a date variable dateDeNaissance with a valid date string from a form (the variable value is intercepted successfully).
  2. Attempt to calculate the age using both the provided expressions.
  3. Observe that both expressions return null.

Expected Behavior:

The expressions should return the number of years between the current date and dateDeNaissance, but both return null.

Additional Information:

  • Example Input:
    • dateDeNaissance: "2023-10-11T12:00:00+01:00"

Questions:

  • Why are both expressions returning null?
  • Is there a specific configuration needed to correctly parse the dateDeNaissance input?
  • Are there alternative methods or best practices for calculating the age in years using DMN JS?

Hi @OUSSAMA_TAYARI_0
I’m not an expert on the topic but I think there are two issues for the Expressions Used.
1 - The conversion function should be date and time because your input is in the data and time format
2 - year should be years

Try it like the following: (years and months duration(date and time(dateDeNaissance), now())).years

Regards,
Alex

1 Like

Thanks @Alex_Voloshyn for you responce.
i tested also this expression :

year(date and time(now())) - year(date and time(dateDeNaissance))

and also the return is null.

Can you try the one that I suggested?
I tested it and it works for me: (years and months duration(date and time(dateDeNaissance), now())).years

1 Like

From Alex’s suggestions, putting the information into:

The formula:
years and months duration(date and time(dateDeNaissance), now())
with an input of

{
  "dateDeNaissance": "2023-10-11T12:00:00+01:00"
}

Returns P1Y (1 Year)

The playground is really useful for understanding what’s going on.
It will turn out errors (like

* [FUNCTION_INVOCATION_FAILURE] Failed to invoke function 'date': Failed to parse date from '2023-10-11T12:00:00+01:00'
* [FUNCTION_INVOCATION_FAILURE] Failed to invoke function 'years and months duration': Illegal arguments: 'null', '2024-11-04T16:47:45.247119038@Etc/UTC'
3 Likes

@Alex_Voloshyn
Thanks !
the expression you provided works perfectly.

1 Like

thanks @GotnOGuts that playground is helping me a lot.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.