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

@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