Get difference for two dates in Months and Days

Hi,

I am trying to get difference between two dates in Months and Days. What I have tried as follows:

Var 1 = 3/10/2023 (It is as “Now”)
Var 2 = 17/09/2022

I tried below:

DateDiff(DateInterval.Month,DateTime.ParseExact(Var2,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture),Var1)

This would give us result of 13 months, which is a rounded month. I want to get the decimal/fraction of it to be more accurate.

You can see
Main.xaml (7.9 KB)
use
cINT(DateDiff(DateInterval.Day,date1,today))
regards,

Hi
These are the expression you can use in assign activity

In days

int_days = DateDiff(DateInterval.Day, DateTime.ParseExact(Strinput.ToString, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture),DateTime.now())

Output type is int32
Input you can pass the string to variable named Strinput = “17/09/2022”

In months

int_months = DateDiff(DateInterval.Month, DateTime.ParseExact(Strinput.ToString, “dd-MMM-yyyy”, System.Globalization.CultureInfo.InvariantCulture),DateTime.now())

Output type is int32

You can get to know all sort of Datetime expressions from this post

Hope this helps

Cheers @aqiffm

Hi @aqiffm

MonthsDifference= CDec(DateDiff(DateInterval.Day, DateTime.ParseExact(Var2, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture), Var1) / 30.0)

This expression calculates the month difference as a decimal and explicitly converts it to the Decimal data type using CDec.

DaysDifference= CInt(DateDiff(DateInterval.Day, DateTime.ParseExact(Var2, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture), Var1) Mod 30)

This expression calculates the remainder of the days difference after dividing by 30 to get the days remaining.

Remember to replace Var1 and Var2 with your actual date variables in UiPath.

Note: Both the variables are of Datatype System.Int32

Hope it helps

1 Like

If I do in Months, it will round off the months. Difference between two dates would be 12 Months and 17 days. I dont want it to round off

1 Like

Fine

It doesn’t come together with expression
It has to be rather concatenated like this

Use this expression in assign activity

date1 = DateTime.ParseExact(Strinput.ToString, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture)
date2 = DateTime.Now

int_months = Math.Floor((date2.Year - date1.Year) * 12 + (date2.Month - date1.Month))

int_days = (date2 - date1.AddMonths(int_months)).Days

Then use a assign to concatenate like this

Stroutput = “Difference: " + int_months.ToString + " months " + int_days.ToString + " days”

Cheers @aqiffm

1 Like

Hope it’s clarified
@aqiffm

this works, eventho its just estimation but it’s close enough. thank you!

1 Like

this also works, thank you!

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