How to check if the string is in datetime format?

I have a string as per the below format, and i need to confirm that it is in correct datetime format. Can you please suggest the best approach for this?

String : “8/24/23 9:11 AM”

@Harsha_Vemula

Try this DateTime.TryParseExact("DateString", "M/d/yyyy hh:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, nothing)

Cheers

I tried exactly the below
Datetime.TryParseExact(“8/24/23 9:11 AM”, “M/dd/yy hh:mm tt”, cultureinfo.invariantculture, Datetimestyles.none,Nothing)
But this is giving me the output as false.

@Harsha_Vemula

Instead of hh:mm use h:m

Cheers

1 Like

Hi @Harsha_Vemula

inputString As String = "8/24/23 9:11 AM"
isDateTimeValid As Boolean = DateTime.TryParseExact(inputString, "M/d/yy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, Nothing)

If isDateTimeValid Then
    ' String is in the correct datetime format
    LogMessage("Datetime is valid.")
Else
    ' String is not in the correct datetime format
    LogMessage("Datetime is not valid.")
End If

Hope it helps!!

1 Like

Hi @Harsha_Vemula

Try this

DateTime.TryParseExact("8/24/23 9:11 AM", "M/d/yy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, Nothing)

I hope it works!!

1 Like

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