Assign: String was not recognized as a valid DateTime. Convert "12. 5. 2023 16:22 " to date format and compare it with another date

HI I have String format like this “12. 5. 2023 16:22” and like this “26. 4. 2021 2:15” and i need to convert them in date and compare them which date is older.
Any help?

Hi @pateto, welcome to the Community.

You need to convert the String data to DateTime & then proceed with the comparison. You can convert it like this:

date1 = DateTime.ParseExact(dateString1, "dd. M. yyyy H:mm", System.Globalization.CultureInfo.InvariantCulture)

date2 = DateTime.ParseExact(dateString2, "dd. M. yyyy H:mm", System.Globalization.CultureInfo.InvariantCulture)

Then, you can proceed with the comparison & determination of old date like this in If condition:

If date1 < date2 Then
    oldestDate = date1
Else
    oldestDate = date2
End If

Hope this helps,
Best Regards.

@pateto

Welcome to the community

you can do the following to covert the given string to date and later can use greater than and less than symbols to check which is greaters

DateTime.ParseExact("12. 5. 2023 16:22","d. M. yyyy H:m",System.Globalization.CultureInfo.InvariantCulture)

Conversion:
image

Comparison:
image

Hope this helps

cheers

Hi,

Hope the following sample helps you.

DateTime.ParseExact(strD1,"d. M. yyyy H:m",System.Globalization.CultureInfo.InvariantCulture)

Sample20230515-7L.zip (2.5 KB)

Regards,

createdDate = System.Text.RegularExpressions.Regex.Match(createdDate, “(\d{1,2}.\s*\d{1,2}.\s*\d{4})\s+(\d{1,2}:\d{2})”).Groups(1).Value.Replace(" ", “”)

createdDate = Datetime.parseexact(createdDate,“d.M.yyyy”,system.globalization.cultureinfo.invariantculture).ToString(“dd/MM/yyyy”)

I solved by using regrex

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