When I run:
Date.Parse(Now.ToString("M/yy/d"), New Globalization.DateTimeFormatInfo() With {.ShortDatePattern = "M/yy/d"})
I am getting the error:
Could not determine the order of year, month, and date from 'M/yy/d'.
Is this (strange) format somewhere prohibited? Is it a bug?
Note
I do not know why this format is required.
Edit
Proposed Date.ParseExact(Now.ToString("M/yy/d"), "M/yy/d",New Globalization.DateTimeFormatInfo() )
gives:
String was not recognized as a valid DateTime.
Edit2
I am using NET Framework 4.0 (windows).
The problem is a result of two components:
/
)./
is used as part of a format string in DateTime.Parse
or DateTime.ParseExact
it is not translated as a literal forward slash but rather as the date separator character from the specified format provider, whatever that is (if you want it to be taken literally, you must use the "escape sequence" %/
).So there are many ways to do this correctly:
Date.ParseExact(Now.ToString("M/yy/d"), "M%/yy%/d", Nothing)
This simply tells ParseExact
to interpret the slashes literally.
Date.ParseExact(Now.ToString("M/yy/d"), "M/yy/d",
New Globalization.DateTimeFormatInfo() With {.DateSeparator = "/"})
This tells ParseExact
to use a literal slash as the date separator, changing its default behavior for your current culture.
As an aside: I 'm using ParseExact
instead of Parse
because it is the better fit when you know that the input has a specific format; Parse
attempts to determine the format on its own using heuristics, which is not appropriate in this case.