How to calculate how many months between two dates

How can I compare two dates to calculate how many full months have passed.

I have a date “2022-02-16” the other can be “2022-03-16” in this scenario 1 full month has passed but if the second date is “2022-03-15” then 0 full month has passed.

and again if it was “2022-02-16” the other can be “2022-04-16” then it would be two full months.

What’s the best way to do this.

@duaine.b
Code: DateDiff(DateInterval.Month, Convert.ToDateTime( “2016-01-30”),Convert.ToDateTime(“2016-05-30”))

@raja.arslankhan Hi thanks for this but it doesn’t work for me because if i do DateDiff(DateInterval.Month, Convert.ToDateTime( “2016-01-30”),Convert.ToDateTime(“2016-05-29”)) it should return 3 as it hasn’t been 4 full months. Do you know how to have a compare function that compares the days with the months also to ensure a full month has passed

Hi @duaine.b your case is really tricky, I am trying to found solution for you.

@duaine.b

Then instead of taking difference… try doing add months and then compare the output with the final date you have

In a loop use the below formula and compare with the final date if it crossed or no…if crossed then use the previous number as Difference

Convert.ToDateTime( “2016-01-30”).AddMonths(4) - 05/30/2016 which is greater than the given date 2016-05-29…so the months can be assumed as 3

image

So basically you will use a while loop with condition on date

cheers

Hi @duaine.b

Check this below attached workflow and please verify with your input dates & let us know,
Uipath_GetTotalMonths.xaml (7.4 KB)

For more info please visit this below link,

Hope this may help you :slight_smile:

3 Likes

Hi @duaine.b

How about this DateDiff expression ?

DateDiff(DateInterval.Month, "2022-02-16", "2022-03-16")

Regards
Gokul

we assume that all date strings are converted into a DateTime (e.g. CDate, DateTime.Parse…, Convert.ToDateTime… methods)

the missing correction of incomplete months we can do like:

Other series:
grafik

We need to ensure that date2 > date1

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.