VB.Net DateTime.Parse and ParseExact don't work
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)
.
Chiwda
Although I never studied Computer Science, I have been programming on and off for 25+ years - since I was an undergrad. My first language was Fortran IV (the output was on punch cards!), then went on to Lisp, VB.Net, C, C++, Python, PHP, JavaScript etc.
Updated on June 05, 2022Comments
-
Chiwda almost 2 years
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.
-
Chiwda almost 12 yearsThanx much! I was under the impression that the format string i.e. "MM-dd-yyyy" was supposed to format the result not the input string. In hindsight, that was dumb of me, because how would the function know what was being input? Duh! {-)