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

Hello,

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

Similarly
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.

@qwerty1

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`

cheers

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
Then
’ Dates are within 3 years of one another
’ Your logic here for true case
LogMessage(“Dates are within 3 years of one another (true).”)
Else
’ 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:
``````output<3
Then
Print True
Else
Print False
``````