Is there a way to calculate the different months between two dates in the outsystems?

Hi Yi, 

There is no built-in function for that. And is not easy to define one, as different months have different number of days, and February changes its number of days depending on the year. 

You have DiffDays, that will give you the number of days between 2 dates. 

If you define a month as 30 days, you can than divide the result of DiffDays by 30 and find a rough approximation. 

Hope this helps. 

Cheers 

Yi Wang wrote:

Is there a way to calculate the different months between two dates in the outsystems?

Hi Yi !

you can also add a function to calculate the integer difference if that's no issue for you.
So, if you create a function in Outsystems with 2 input parameters (startdate, enddate),
you should be able to get the difference in months with the following expression:

SyntaxEditor Code Snippet

Abs(12 * (Year(StartDate) - Year(EndDate)) + Month(StartDate) - Month(EndDate))

This will give you an integer value and not an aproximation in decimals.

----

Joao


Liked your solution, João. 

It will give you funny things like:

2019-08-01 / 2019-08-29 => 0 months

2019-08-29 / 2019-09-01 => 1 month

But depending on what is being requested, like not the exactly difference in months, but only a number that indicate if the second date is in the same month as the first on in which month after the first, it works perfectly. 

And in most cases will make much more sense. 

Cheers 

I would get the years and months for each date and save 12*years+month as the months that existed since the start.

Months1=12*Year(Date1)+Month(Date1)

Months2=12*Year(Date2)+Month(Date2)


The formula abs(Months2-Months1)+1 will say how many months exist. 1 if they are in the same month, 2 if they are consecutive months, 3 if there is a month between, and so on.


I use the absolute because month1 can be greater or smaller than month2.

Yi Wang wrote:

Is there a way to calculate the different months between two dates in the outsystems?

Hi Yi Wang,


To calculate the difference between months between 2 dates, you have to consider that you'll have a decimal value when the difference is not an exact Month.

I've made a function to perform what you need:


If you create a function with that exact code, you should be able to have what you need.

I tried to comment everything in order for you to understand what it's being done on each step.


Please check if this works for you ;)


Kind regards,

Cláudio Oliveira



Hi, 

You can do something about it using these functions.

but exists date variations and  
are not covered in this solution.

Regards,

Trunc(DiffDays(Data1,Data2) / 30)

Leonardo Nascimento wrote:

Hi, 

You can do something about it using these functions.

but exists date variations and  
are not covered in this solution.

Regards,

Trunc(DiffDays(Data1,Data2) / 30)


Hi Leonardo, this solution although simpler, will always consider that a month have 30days.


That's why I've made a more complex solution above which considers the number of days for the months that are being compared, that's the tricky part.

Cláudio Oliveira wrote:

Yi Wang wrote:

Is there a way to calculate the different months between two dates in the outsystems?

Hi Yi Wang,


To calculate the difference between months between 2 dates, you have to consider that you'll have a decimal value when the difference is not an exact Month.

I've made a function to perform what you need:


If you create a function with that exact code, you should be able to have what you need.

I tried to comment everything in order for you to understand what it's being done on each step.


Please check if this works for you ;)


Kind regards,

Cláudio Oliveira




thanks a lot

Yi Wang wrote:

Cláudio Oliveira wrote:

Yi Wang wrote:

Is there a way to calculate the different months between two dates in the outsystems?

Hi Yi Wang,


To calculate the difference between months between 2 dates, you have to consider that you'll have a decimal value when the difference is not an exact Month.

I've made a function to perform what you need:


If you create a function with that exact code, you should be able to have what you need.

I tried to comment everything in order for you to understand what it's being done on each step.


Please check if this works for you ;)


Kind regards,

Cláudio Oliveira




thanks a lot


Happy to help ;)

Yi Wang wrote:

Is there a way to calculate the different months between two dates in the outsystems?


Happy to see the solution for it.