Validate Input DateTime C#
How can I validate the DateTime
(input) to be in format of DD/MM/YYYY HH:MM
in C#
I need to throw 开发者_StackOverflow社区an error if the specified format doesn't match the above one.
Have a look at using DateTime.TryParseExact Method
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
You could also try DateTime.ParseExact - this automatically throws FormatException if the input is not in specified format:
var dt = DateTime.ParseExact(dtString, "dd/MM/yyyy hh:mm", new CultureInfo("en-US"));
精彩评论