## Years between function

4
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

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)
now.setTime(comparedate);
Calendar dob = Calendar.getInstance();
dob.setTime(birthdate);
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) {
age--;
} else if (month1 == month2) {
int day1 = now.get(Calendar.DAY_OF_MONTH);
int day2 = dob.get(Calendar.DAY_OF_MONTH);
if (day2 > day1) {
age--;
}
}
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).

2

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.

2

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
2.4.6: http://rapidshare.com/files/414836267/CalculateYearDifference.mmf
2.5.0.1: http://rapidshare.com/files/414837081/CalculateYearDifference.mpk

2

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

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