-5

I have date field in my Windows form and that is a user input. I need to validate if that date input is in mm/dd/yyyy format and need to throw an error if it is not. How Do I do that. I have no idea how to do that.

Please help.

1
  • 3
    Try looking up the documentation for DateTime.ParseExact or DateTime.TryParseExact. Mar 15, 2016 at 10:52

1 Answer 1

3

The following code should do the trick:

DateTime date;
if (DateTime.TryParseExact(text, "dd/MM/yyyy",
                           CultureInfo.InvariantCulture,
                           DateTimeStyles.None,
                           out date))
{
   // Date is valid
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.