I have a filter field that allows to search by partial or full date that can be entered in different formats - that is, whichever way the user wants to enter it, so TryParseExact doesn't look like the best option.
The problem is that the following code:
DateTime.TryParse(fromDate.Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateFromValue)
parses everything but yyyy strings. E.g. "05/1980" and "1980/05" are recognized as {1/05/1980 12:00:00 AM}, but "1980" fails.
I suspect the parser has no way to distinguish between, say, yyyy and ffff when all it gets is a 4-digit sequence, but I still need to get this working. Is there perhaps a more elegant way to do it than
if(!DateTime.TryParse("1980", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
DateTime.TryParseExact("1980","yyyy",CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
or am I doing it the wrong way altogether?
Thank you in advance!