2

I have a string which is a representation of DateTime:

string dateTime = "20110801";

Now I want to use DateTime.Parse(dateTime);

It throws an Exception stating that dateTime is not in a valid format.

Can anyone tell me how it can be done??

2 Answers 2

5

You need to call ParseExact with the format string yyyyMMdd.

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

Comments

3

You can use DateTime.ParseExact, which takes in the format as a parameter.

For example:

DateTime.ParseExact("20110801", "yyyyMMdd", CultureInfo.CurrentCulture)

2 Comments

SLaks beat me. Always a bridesmaid, never a bride.
Thanks both of you for answering my question. That was the simplest solution I needed.

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.