Compare two dates with different formats(string: "dd.MM.yyyy" and generic value "dd/MM/yyyy hh:mm:ss")

Hello everybody,

I need to compare two dates with different formats and find out, if date1 is earlier than date2.
Date1 is from SAP in stringformat “dd.MM.yyyy” and date2 is from excel in generic value(datetime) “dd/MM/yyyy hh:mm:ss”.

First I got the error that the string “dd.MM.yyyy” doesn’t have the right format for datetime. I tried to add 00:00:01 to the string and then convert to datetime, but it still doesn’t recognize it as right format.

Does anybody know how to convert and compare both dates (in an if activity) in order to find out if date1 is earlier than date2?

Thank you!

@silvia93
we can handle as following:

see defined 2 test strings and a string array with the 2 different dateformats below:

Parsing we can do within an assign like this:
DateTime.ParseExact(strDate1,Formats,CultureInfo.InvariantCulture,DateTimeStyles.None)
DateTime.ParseExact(strDate2,Formats,CultureInfo.InvariantCulture,DateTimeStyles.None)

And will receive following
grafik

the yellow lines are the statement that can be used within the condition block of an if

Kindly note:

  • As we used the dateformats within a string array we have to define the last forth parameter as well from the method signature. But with this we can use the same call for both formats (sure different datestrings are to pass)
  • an alternate for a single format parse would be like DateTime.ParseExact(strDate1,"dd.MM.yyyy",CultureInfo.InvariantCulture)
  • ensure that system.Globalization is imported:
    grafik

Let us know your feedback

5 Likes

Hey ppr. Big up for always making sound and to the point answers :slight_smile:

Great, it works! Thank you!

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