Microsoft Developer
Microsoft Developer

Reputation: 5459

Error in converting string to datetime in C#.net

I am getting an error in converting string to date in C#. The error is:
string is not valid datetime.
Below is the code that I am using to convert string into datetime.

string[] DateFormat = { "dd-MM-yyyy", "dd/MM/yyyy", "MM-dd-yyyy", "MM/dd/yyyy" ,"dd-MMM-yy"};
            VendorSinceDate = DateTime.ParseExact(dtresult.Rows[0]["VendorSinceDate"].ToString(),DateFormat,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None);

//dtresult.Rows[0]["VendorSinceDate"].ToString()="06-Jun-12 12:00:00 AM";

Upvotes: 0

Views: 238

Answers (1)

Dogmang
Dogmang

Reputation: 727

None of your possible DateFormats include the time, but the string you're trying to convert does.

You need to add a matching DateFormat, e.g. dd-MMM-yy hh:mm:ss tt

Upvotes: 2

Related Questions