How to check if a date is in mm / DD / yyyy format or not in C #

How to check if a date is in mm / DD / yyyy format or not in C #?

+5
source share
1 answer

Note. I assume you asked if the string is a valid date in the format "MM / dd / yyyy". The format DateTimeitself has no format, so you cannot check it.

Use DateTime.TryParseExactto analyze it:

string text = "02/25/2008";
DateTime parsed;

bool valid = DateTime.TryParseExact(text, "MM/dd/yyyy",
                                    CultureInfo.InvariantCulture,
                                    DateTimeStyles.None,
                                    out parsed);

Note that I changed your format string to what I think you mean - I doubt that you really meant that the first bit should be minutes, for example.

If you do not want an invariant culture, specify another :)

+17

All Articles