SaiBand
SaiBand

Reputation: 5355

.NET Parsing string into DateTime

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??

Upvotes: 2

Views: 152

Answers (2)

schummbo
schummbo

Reputation: 900

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

For example:

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

Upvotes: 3

SLaks
SLaks

Reputation: 887195

You need to call ParseExact with the format string yyyyMMdd.

Upvotes: 5

Related Questions