1

I searched on the internet for a while now, but every answer i found, didn't worked for me.

I am trying to convert a string to a datetime, but every time i try different things, i get the exeption: System.FormatException: 'String was not recognized as a valid DateTime.

string temp = Request.QueryString["begintijd"];
DateTime test = DateTime.ParseExact(temp, "dd/MM/yyyy", CultureInfo.InvariantCulture);

The value of temp is: 05/22/2017 15:00:00

I also tried "MM/dd/yyyy" but that also fails.

3
  • MM/dd/yyyy hh:mm:ss Commented May 22, 2017 at 18:11
  • @gravity - Since his time is in 24h format, hh will not work as it's used for 12h. In this instance, HH is needed. Commented May 22, 2017 at 18:18
  • Thanks for the help @gravity! Just the change noted by @ben buurstra and its works :D Commented May 22, 2017 at 18:21

1 Answer 1

2

In your example, where temp = 05/22/2017 15:00:00, you need your 'format' in ParseExact to match that syntax.

So I would try this, DateTime test = DateTime.ParseExact(temp, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariateCulture);

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

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.