Check if date string is within 3 years of another date string


I’m trying to determine if two dates are within 3 years of one another. The date could -3 years, or up to 3 years.

For example:
str1 = 01/01/2023
str2 - 01/01/2020

Date diff shows this as -3. This should be true

str1 = 01/01/2020
str2 - 01/01/2023

Date diff shows 3 this should also be true.

Anything over 3 years either side should be treated as false.

I’ve tried the following, but it only works in one direction:

datediff(dateInterval.Year, DateTime.ParseExact(str1, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture), DateTime.ParseExact(str2, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture))<3

@Palaniyappan i read your ‘All about DateTime’ post, but i couldn’t find a scenario that covered the above.

Any help would be appreciated.


try this with Math.abs

Math.Abs(datediff(dateInterval.Year, DateTime.ParseExact(str1, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture), DateTime.ParseExact(str2, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)))<3


1 Like

Hi @qwerty1

=> Input date strings
=> Assign → str1 (String) = “01/01/2023”
=> Assign → str2 (String) = “01/01/2020”

=> Assign → date1 (DateTime) = DateTime.ParseExact(str1, “MM/dd/yyyy”, CultureInfo.InvariantCulture)
=> Assign → date2 (DateTime) = DateTime.ParseExact(str2, “MM/dd/yyyy”, CultureInfo.InvariantCulture)

=> Assign → diffYears (Integer) = Math.Abs(date1.Year - date2.Year)

’ Check if the absolute difference is less than or equal to 3 years
=> Assign → BoolFlag (Boolean) = diffYears <= 3

=> If BoolFlag result
’ Dates are within 3 years of one another
’ Your logic here for true case
LogMessage(“Dates are within 3 years of one another (true).”)
’ Dates are more than 3 years apart
’ Your logic here for false case
LogMessage(“Dates are more than 3 years apart (false).”)

Hope it helps!!

1 Like

Hi @qwerty1

   str1 = "01/01/2023"
   str2 = "01/01/2020"

    date1 = DateTime.ParseExact(str1, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
   date2 = DateTime.ParseExact(str2, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

    timeSpan = date1 - date2;
    yearsDifference = Math.Abs(timeSpan.Days) / 365

    bool isWithin3Years = yearsDifference <= 3

I hope it helps!!

1 Like

Hi @qwerty1

Assign: dateStr1 = “01/01/2023”
Assign: dateStr2 = “01/01/2020”

Assign: result = DateTime.ParseExact(dateStr1, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture).Subtract(DateTime.ParseExact(dateStr2, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)).Duration().TotalDays <= 3 * 365

Hope it helps!!

1 Like

Hi @qwerty1

Use this conditions in Assign Activity:

str1= "01/01/2020"               (Datatype: System.String)
str2= "01/01/2023"                (Datatype: System.String)
output= datediff(dateInterval.Year, DateTime.ParseExact(str1, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture), DateTime.ParseExact(str2, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture))           (Datatype: System.Int64)
  1. Use If activity and give the below Condition:
         Print True
         Print False

Uploaded Workflow for reference too
Sequence6.xaml (8.3 KB)

Hope it works!!

1 Like

Thank you all for your suggestions. I ended up using @Anil_Gorthi solution as only small modifiacation needed to my original code. Thank you all for the time taken to post.
Best wishes for the weekend ahead

1 Like

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