guiomie
guiomie

Reputation: 5148

Convert string to datetime fails

I can't seem to find how to convert a string to a datetime. Here is what my date looks like: "23-nov-12".

I tried the following:

DateTime convertedDate = DateTime.ParseExact("dd-MMM-yy", "23-nov-12", CultureInfo.InvariantCulture);

and also

DateTime convertedDate = DateTime.ParseExact("0:d-MMM-yy", "23-nov-12", CultureInfo.InvariantCulture);

But I always get the following error:

String was not recognized as a valid DateTime.

Upvotes: 0

Views: 1219

Answers (3)

Rizwan Ahmed
Rizwan Ahmed

Reputation: 470

This code Works...

  string dt = "23-nov-12";
  Console.WriteLine(Convert.ToDateTime(dt));

It produces 11/23/2012 12:00:00 AM as output

Try It!

Upvotes: 1

SLaks
SLaks

Reputation: 887195

You got the parameters backwards.
The input string goes first.

DateTime.ParseExact("23-nov-12", "dd-MMM-yy", CultureInfo.InvariantCulture)

Upvotes: 4

qJake
qJake

Reputation: 17119

Your arguments are in the wrong order. The date goes first.

DateTime convertedDate = DateTime.ParseExact("23-nov-12", "dd-MMM-yy", CultureInfo.InvariantCulture);

See: DateTime.ParseExact()

Upvotes: 8

Related Questions