Years between function

The years between function does not exist in the Microflow expressions. How to I determine someones age (formated in years, 32 not 32,17263459)
5 answers

The following Java code can be wrapped easily in an action, and calculates exactly what you want it to do:

public static long getAge(Date birthdate, Date comparedate) {
    if (birthdate == null)
        return -1L; 

    Calendar now = Calendar.getInstance();
    if (comparedate != null)
    Calendar dob = Calendar.getInstance();
    if (dob.after(now)) 
        return -1L;

    int year1 = now.get(Calendar.YEAR);
    int year2 = dob.get(Calendar.YEAR);
    long age = year1 - year2;
    int month1 = now.get(Calendar.MONTH);
    int month2 = dob.get(Calendar.MONTH);
    if (month2 > month1) {
    } else if (month1 == month2) {
      int day1 = now.get(Calendar.DAY_OF_MONTH);
      int day2 = dob.get(Calendar.DAY_OF_MONTH);
      if (day2 > day1) {
    return age;     

You can leave the compare date empty (null) to use the current time. The code is based on this thread.

Btw I guess that the much shorter code

Math.floor((comparedate.getTime() - birthdate.getTime()) / 31556952 * 1000)

gives a quite accurate estimate. (based on wikipedia:)

An average Gregorian year is 365.2425 days = 52.1775 weeks = 8,765.82 hours = 525,949.2 minutes = 31,556,952 seconds (mean solar, not SI).


Best method would be to use 'weeksBetween' and divide it by 52 and finish off with either round(), floor() or ceil() depending on how you want to round off the years.

Edit: It is actually 52.177457 weeks averaging over leap years, adding 1 day every 4 years.


The java action michel wrote is correct. Even though it didn't have to be so complicated. The java calendar (or gregorian calendar) provides the year between function as well it does exactly the same as the function from Michel.

But because your working with the Mendix modeler you always want to use a microflow, below this text are 2 download links from which you can download a microflow export. The flow has two dates as input and as result you will receive an integer which indicates the number of years between the dates


I was looking for the same function and read this forum item. But it is simple: download the CommunityCommons, there is a function called YearsBetween


There is also the possibility to use the toString method on a date. First format the date to a string then create a substring of that string and parse that substring to an integer. It would then look something like this:

parseInteger(substring(trim(toString($inputEvent/date)),0,4)) - parseInteger(substring(trim(toString($inputEvent2/date2)),0,4))

Another solution would be to make a java action that retreives the year from any date :]