How to compare two dates and return the largest

How to compare two dates and return the largest.
Date format: ddmmyyyy

Create two DateTime objects and compare them. You can use DateTime.Parse() to create the dates, and a simple IF activity to compare them: IF date1 > date2.

2 Likes

Hi
hope these steps would help you resolve this
if the input is in string value named str1 and str2 with date format as ddMMyyyy

then use this expression in the IF condition like
Datetime.ParseExact(str1.tostring,“ddMMyyyy”,System.Globalization.CultureInfo.InvariantCulture) > Datetime.ParseExact(str2.tostring,“ddMMyyyy”,System.Globalization.CultureInfo.InvariantCulture)

if true it will go to THEN part where we can use WRITE LINE activity and mention as str1.ToString or goes to ELSE part where use str2.ToString

Cheers @Suporte4

1 Like

@Suporte4
As an alternate populate the two dates (Conversion as @Palaniyappan has described) into a datetime Array. Then sort this array and retrieve the largest by its position (e.g. last for ASC sorting). This approach let ommit the if else part

Sorting: arrdates.OrderBy(function (x) x).Last()

1 Like

Thank you! It’s working!
For other people who needs this solution, I have to make a little correction in the string:
it’s missing “t” in “InvariantCulture”

Datetime.ParseExact(str1.tostring,“ddMMyyyy”,System.Globalization.CultureInfo.InvarianCulture) > Datetime.ParseExact(str2.tostring,“ddMMyyyy”,System.Globalization.CultureInfo.InvarianCulture)

still missing buddy
i modified in my comment itself

Cheers @Suporte4

1 Like

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