Need to find the month diff

Need to find the month difference between two date
09/31/2023 and 12/31/2023

The month diff is 3 months

@sruthesanju

Int32.Parse(DateDiff("m", DateTime.ParseExact("09/30/2023", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture), DateTime.ParseExact("12/31/2023", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)).ToString())

image

Assign variables -->“09/30/2023”,“12/31/2023”

Hi @sruthesanju

You given one date which is 09/31/2023 in september month, there is only 30 days but you have given 31, change the date and follow the below,

Try this:

- Assign -> strInput1 = "09/30/2023"
- Assign -> strInput2 = "12/31/2023"
- Assign -> Output = DateDiff(DateInterval.Month, DateTime.ParseExact(Strinput1.ToString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.ParseExact(Strinput2.ToString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture))

Note : Output is the Int64 datatype variable.

Check the below image for better understanding,

Hope it helps!!

Hi @sruthesanju

Try this:

Date1 = DateTime.ParseExact(dateString1, dateFormat, System.Globalization.CultureInfo.InvariantCulture)

Date2 = DateTime.ParseExact(dateString2, dateFormat, System.Globalization.CultureInfo.InvariantCulture)


DateDiffMonth = DateDiff(DateInterval.Month, Date1, Date2)

Hope it will helps you :slight_smile:
Cheers!!

@sruthesanju

you can try this

cint(DateDiff(DateInterval.Month,CDate(date1),CDate(date2)))

To find the month difference between two dates in UiPath, you can use VB.NET code within an ‘Assign’ activity. Here’s a step-by-step process:

  1. Parse Dates*: Convert the string representations of the dates into DateTime objects.

Dim date1 As DateTime = DateTime.ParseExact(“09/30/2023”, “MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture)
Dim date2 As DateTime = DateTime.ParseExact(“12/31/2023”, “MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture)

Note: The date “09/31/2023” is not valid since September has only 30 days. I’ve used “09/30/2023” instead.
2. Calculate Month Difference*: Use the DateDiff function or subtract the months and account for the year difference.

Dim monthDiff As Int32 = ((date2.Year - date1.Year) * 12) + date2.Month - date1.Month

  1. Assign to Variable*: Assign the result to an integer variable that will hold the difference in months.
  2. Use the Variable*: Use this variable as needed in your workflow, for example, to log the message or perform other operations.

This will give you the difference in months between the two dates.

Depends on the precision which is needed

  • full completed month like in your case: End Of Month - End of Month
      1. JAN 2023 - 28. FEB 2023 = 1 Month
    • 01 JAN 2023 - 25.JAN 2023 = 0.806 Month
  • Or only counting when other months are touched
      1. JAN .2023 - 05. FEB 2023= 1 Month
      1. DEZ 2023 - 27. DEZ 2023 - 0 Month

We ask for this detail as DateDiff is not handling the fractions:
grafik

EDITED: Fixing Typos, Added example of a fraction

Hi @sruthesanju

FirstDate = 09/31/2023
SecondDate = 12/31/2023
Create a Integer Variable and name as monthDifference.

Use Assign Activity
monthDifference = DateDiff(DateInterval.Month, FirstDate, SecondDate)

hey @sruthesanju

Dim date1 As DateTime = DateTime.ParseExact("09/31/2023", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
Dim date2 As DateTime = DateTime.ParseExact("12/31/2023", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)

Dim monthDiff As Integer = Math.Abs(DateDiff(DateInterval.Month, date1, date2))