## 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)
4

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

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

1

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 :]