I have tried all combinations of DateTime.Parse and ParseExact and they don’t work. I keep getting the message “{“String was not recognized as a valid DateTime.”}” for both those functions. I have also tried Convert.ToDateTime and that don’t work. Just to check if I wasn’t dreaming, I wrote the following code:
Dim ExpiryDate As Date = System.DateTime.Now
Dim provider As New CultureInfo("en-US")
Try
Dim strDate As String = Convert.ToString(ExpiryDate)
ExpiryDate = DateTime.ParseExact(strDate, "mmddyy", provider)
Catch ex as exception
'ex here says "{"String was not recognized as a valid DateTime."}"
End Try
I just want to take string in this form mm-dd-yyyy or mm/dd/yyyy and convert it to a date so I can test (greater than) against another date. Any help appreciated.
First of all, the format string needs to match the string being parsed. Secondly, an “m” in the format string stands for minutes, not month. If your date is formatted as mm-dd-yyyy, try
DateTime.ParseExact("04-08-2011", "MM-dd-yyyy", provider).