0

How can I validate the DateTime (input) to be in format of DD/MM/YYYY HH:MM in C#

I need to throw an error if the specified format doesn't match the above one.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.