1

I need to calculate the number of months between to dates and have an integer value returned. I noticed there are methods minutesBetween and weeksBetween, is there away to calculate monthsBetween?

asked

Adam Fothergill

4 answers

3

Hi Adam, I've realised my answer could be a bit too specific (just for one year boundary, Jan to Dec). A more general one would use similar principle where you can get the values of both year(s) and month(s) of the dates you want to check, so for example...

May/2011 and Jan/2013

you would have something like this... [(Year2 - Year1) * 12 + (Month2 - Month1)]

for the case above you should then end up with 20 months between the two dates.

no direct function but not too much to set up. LR.

answered

Luis Rodrigues

0

There is a month-from-dateTime function(xpath though).

In microflows I tend to just grab the month number in a couple of variables and get the difference between the variables.

e.g. : parseInteger(formatDateTimeUTC([%CurrentDateTime%], 'M')) -> this will return integer (1)

If you have another date, you can establish which date is the first which is second and subtract accordingly. Hope it helps. LR.

answered

Luis Rodrigues

0

There is no microflow expression monthsBetween, so you need to calculate it. I tried this approach:

- use daysBetween with the two dates
- divide them into 30 days (average days in a month)
- use round

Result: `round(daysBetween($date1,$date2)) div 30)`

It's not completely accurate but in my case that wasn't really important.

answered

Samet Kaya

0

It is strictly mathematically not possible to calculate months between because months have no fixed length. However it is well understood by humans if you say 3,5 months.

Here is my JavaAction that calculates the complete months between two dates (Parameters startDate end endDate).

imports

```
import java.util.Calendar;
import java.util.Date;
```

user code

```
// BEGIN USER CODE
Calendar end = Calendar.getInstance();
end.setTime(endDate);
Calendar start = Calendar.getInstance();
start.setTime(startDate);
return (long) ((end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12 +
(end.get(Calendar.MONDAY)- start.get(Calendar.MONDAY)) +
(end.get(Calendar.DAY_OF_MONTH) > start.get(Calendar.DAY_OF_MONTH) ? 1: 0));
// END USER CODE
```

Use at your own risk.

answered

Chris de Gelder